1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
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)
}
|