-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
239 lines (191 loc) · 4.05 KB
/
main.go
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Basic Go types:
// bool, byte, string
// int, int8, int16, int32, int64
// uint, uint8, uint16, uint32, uint64
// float32, float64
package main
import (
"encoding/binary"
"fmt"
"math"
"math/rand"
"os/user"
"time"
)
func basics() {
// Reading an integer from stdin
var x int
fmt.Scanf("%v\n", &x)
// Reading an integer and a char right after it
var c byte
fmt.Scanf("%v%c\n", &x, &c)
fmt.Println(string(c))
// Writing to stdout
fmt.Println("Test")
fmt.Print("Test\n")
fmt.Printf("%v %s", x, "Test")
fmt.Printf("%b", x) // Print base 2 of integer x
// Outputting decimal numbers
y := 87.567013
fmt.Printf("%.3f", y) // Prints 87.567
z := 7
fmt.Printf("%03d", z) // Prints 007
// Casting to another type
n := 26
s := float32(n)
fmt.Println(s)
// Loop
for i := 0; i < 3; i++ {
fmt.Println(i)
}
// Random number generation
x = rand.Intn(1000) // A random number between [0,1000)
fmt.Println()
// Some math
u := 7
v := 3
w := math.Ceil(float64(u) / float64(v))
fmt.Println(w)
}
func arrayExample() {
arr1 := [3]int{1, 2, 3}
arr2 := [...]int{4, 5, 6} // unknown size notation
// the following copies arr2 to arr1 element by element
arr1 = arr2
arr2[2] = 23
fmt.Println(arr1, arr2)
// Deleting the i-th element of an array
i := 4
a := [...]int{0, 1, 2, 3, 4, 5, 6}
b := append(a[:i], a[i+1:]...)
fmt.Println(b)
}
func sliceExample() {
arr := [5]int{1, 2, 3, 4, 5}
slc1 := arr[1:4]
slc2 := arr[3:]
slc3 := arr[:4]
slc4 := append(slc2, 99)
fmt.Println(slc1, slc2, slc3, slc4)
// len(arr1) gives the length of the array while cap(arr1) gives the the block of memory reserved for the array.
fmt.Println(len(slc4), cap(slc4))
}
// a map is a dictionary data structure
func mapExample() {
// define the map
var myMap map[string]int
// initialize the map
myMap = map[string]int{}
// add key-value pairs
myMap["a"] = 1
myMap["b"] = 2
// non-existing values will return the default value which is 0 in our case
fmt.Println(myMap["c"])
// to check the existance use the second return value
val, exists := myMap["c"]
if exists {
fmt.Println(val)
}
// iterate over map keys
for k, _ := range myMap {
fmt.Println(k)
}
// to delete a pair from the map
delete(myMap, "b")
delete(myMap, "c") // No errors if the item does not exist
}
func funcHandler() {
type handler func(a []string)
var h handler
h = func(a []string) {
for sig := range a {
fmt.Println(sig)
}
}
h([]string{"a", "b", "c"})
}
func checkOS() {
usr, err := user.Current()
if err != nil {
fmt.Println("Error")
}
homedir := usr.HomeDir
fmt.Println(homedir)
}
// waits for two channels and proceeds based on which channel has something to read
func fibonacci(c, quit chan int) {
x, y := 0, 1
for {
select {
case c <- x:
x, y = y, x+y
case <-quit:
fmt.Println("quit")
return
}
}
}
// uses the above fibonacci function
func channelsSelect() {
c := make(chan int)
quit := make(chan int)
go func() {
for i := 0; i < 6; i++ {
fmt.Println(<-c)
}
quit <- 0
}()
fibonacci(c, quit)
}
// timers use channels
func timer() {
tick := time.Tick(1000 * time.Millisecond)
boom := time.After(5000 * time.Millisecond)
for {
select {
case <-tick:
fmt.Println("Tick")
case <-boom:
fmt.Println("Boom!")
return
}
}
}
// Marshals a list of byte arrays
// Each input array must have less than 65536 bytes (65KB)
func MarshalArrays(arrs ...[]byte) []byte {
size := 0
for _, arr := range arrs {
size += len(arr) + 2
}
i := 0
res := make([]byte, size)
for _, arr := range arrs {
len := len(arr)
binary.BigEndian.PutUint16(res[i:i+2], uint16(len))
copy(res[i+2:i+len+2], arr)
i += len + 2
}
return res
}
// Unmarshals a list of byte arrays
func UnmarshalArrays(input []byte) [][]byte {
var arrs [][]byte
for i := 0; i < len(input); {
len := int(binary.BigEndian.Uint16(input[i : i+2]))
arr := make([]byte, len)
copy(arr, input[i+2:i+2+len])
arrs = append(arrs, arr)
i += len + 2
}
return arrs
}
type animal struct {
}
type duck struct {
animal
featherCount int
}
func main() {
arrayExample()
}