-
Notifications
You must be signed in to change notification settings - Fork 1
/
Slice.go
71 lines (54 loc) · 1.09 KB
/
Slice.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
package main
import "fmt"
func main() {
s := make([]int, 3)
// Similar to array:
s[0] = 1
s[1] = 2
s[2] = 3
fmt.Println(s)
fmt.Println(s[0])
fmt.Println(len(s))
// append function is unique to slices
s = append(s, 4)
s = append(s, 5, 6)
fmt.Println(s)
// slice syntax
fmt.Println(s[1:3])
fmt.Println(s[:3])
fmt.Println(s[1:])
// concise slice definition:
t := []int{100, 200, 300}
fmt.Println(t)
//x := s
//x[0] = 500
//fmt.Println(x)
//fmt.Println(s)
// Use copy to prevent from changing both x and s
x := make([]int, len(s))
copy(x, s)
x[0] = 500
fmt.Println(x)
fmt.Println(s)
// 2-D slices (similar to arrays, although lengths can vary.
ss := make([][]int, 3)
for i := 0; i < 3; i++ {
ss[i] = make([]int, 5)
for j := 0; j < 5; j++ {
ss[i][j] = j + 1
}
}
fmt.Println(ss)
// 3-D slices (similar to arrays, although lengths can vary.
sss := make([][][]int, 3)
for i := 0; i < 3; i++ {
sss[i] = make([][]int, 6)
for j := 0; j < 6; j++ {
sss[i][j] = make([]int, 10)
for k := 0; k < 10; k++ {
sss[i][j][k] = k + 1
}
}
}
fmt.Println(sss)
}