package main // import "fmt" import ( "bufio" "fmt" "os" "strconv" ) func main() { arrays() } func arrays() { var arr [5]int // fills with default value arr[2] = 5 fmt.Println(arr) // properly prints whole array arr2 := []int{1, 2, 3} arr3 := [5]int{1, 2, 3} fmt.Println(arr2, arr3, len(arr2)) arr2d := [][]int{{1, 2}, {10, 20}} fmt.Println(arr2d) bigarr := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} smallerArr := bigarr[:] fmt.Println(smallerArr) smallerArr1 := bigarr[4:] fmt.Println(smallerArr1) smallerArr2 := bigarr[:2] fmt.Println(smallerArr2) smallerArr3 := bigarr[4:6] // start index:end index fmt.Println(smallerArr3) var a []int = []int{1, 2, 3} b := append(a, 4, 5, 6) fmt.Println(b) for k, elem := range bigarr { // equivalent to: for k := 0; k < len(bigarr); k++ fmt.Println(k, elem) } for _, elem := range bigarr { // equivalent to this C# code: foreach(var elem in bigarr) fmt.Println(elem) } } func loops() { i = 0 for i < 5 { fmt.Println(i) i++ } for { if i == 8 { break } else if i%2 == 0 { fmt.Println(i) } i++ } for j := 0; j < 5; j++ { fmt.Println(j) } } func conditionals() { age := 21 if driving_age := 18; age < driving_age { fmt.Println("You cannot drive!") } else if age < 16 { fmt.Println("You cannot use this app!") } else { fmt.Println("You're good") } n := 5 switch n { case 1: fmt.Println("Very cool") case 4: fmt.Println("Not cool") default: fmt.Println("Bruh") } switch { case n > 3: fmt.Println("Yay") default: fmt.Println("Nay") } } func input() { scanner := bufio.NewScanner(os.Stdin) fmt.Printf("Type age: ") scanner.Scan() userInput := scanner.Text() // Always string, duuh // Variable, base (binary, octal, decimal, ..), integer size (8, 16, 32, 64, ..) // _ is an anonymous variable, so it doesn't get anywhere. Second returned variable is error // Bitsize 0 is normal int age, _ := strconv.ParseInt(userInput, 10, 0) fmt.Printf("You were born in %d!", 2021-age) } var i int func variablesAndPrinting() { var msg string msg = "Hello World!" test := 5.98 var num int = int(test) num += 4 num++ str := "Yoo" var str1 = "Bruh" //var v1, v2, v3 int = 45, 2342, 6634 //v4, v5, v6 = 6653, true, "nafd" fmt.Println(msg, num) // Prints both with space in between fmt.Print(str, str1) fmt.Println() /* %T - type * %v - value * %% - escape % character * %t - prints bool * %b, %o, %d, %x - format int as base 2, 8, 10 and 16 repsectively * %e, %f, %g - format float as scientific notation, decimal no exponent (cuts of number) or large exponent (full number) respectively * %f - default width, default precision; %9f - width of 9, default precision; %.2f - default width, precision 2; %9.f - width of 9, precision 0; %9.2f - width of 9, precision 2; - width forces certain string length (use negative number to add spaces to the right), it works with %s and %q also (%-9q) * %d - pads a number with a given digit, so %07d with force width of 7 and add 0s to the number * %s, %q - show string or show string with quotes */ fmt.Printf("%T", num) // Prints type out := fmt.Sprintf("%v %b", str, num) fmt.Println(out) }