-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgoSlices.go
45 lines (37 loc) · 907 Bytes
/
goSlices.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
package main
import "fmt"
func main() {
// Create an empty slice
aSlice := []float64{}
// Both length and capacity are 0 because aSlice is empty
fmt.Println(aSlice, len(aSlice), cap(aSlice))
// Add elements to a slice
aSlice = append(aSlice, 1234.56)
aSlice = append(aSlice, -34.0)
fmt.Println(aSlice, "with length", len(aSlice))
// A slice with length 4
t := make([]int, 4)
t[0] = -1
t[1] = -2
t[2] = -3
t[3] = -4
// Now you will need to use append
t = append(t, -5)
fmt.Println(t)
// A 2D slice
// You can have as many dimensions as needed
twoD := [][]int{{1, 2, 3}, {4, 5, 6}}
// Visiting all elements of a 2D slice
// with a double for loop
for _, i := range twoD {
for _, k := range i {
fmt.Print(k, " ")
}
fmt.Println()
}
make2D := make([][]int, 2)
fmt.Println(make2D)
make2D[0] = []int{1, 2, 3, 4}
make2D[1] = []int{-1, -2, -3, -4}
fmt.Println(make2D)
}