-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11-slices-1.go
56 lines (42 loc) · 1.01 KB
/
11-slices-1.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
// An array has a fixed size. A slice, on the other hand, is a dynamically-sized,
// flexible view into the elements of an array. In practice, slices are much more
// common than arrays.
// https://tour.golang.org/moretypes/7
package main
import (
"fmt"
)
func main() {
s := make([]int, 3)
fmt.Println(s)
s[0] = 1
s[1] = 2
s[2] = 3
fmt.Println(s)
fmt.Println(s[2])
// len is used for getting length of slice
fmt.Println("lenght of slice s is ", len(s))
s = append(s, 4)
s = append(s, 5)
fmt.Println("lenght of slice s is ", len(s))
c := make([]int, len(s))
copy(c, s)
fmt.Println("cpy:", c)
l := s[2:5]
fmt.Println("sl1:", l)
l = s[:5]
fmt.Println("sl2:", l)
l = s[2:]
fmt.Println("sl3:", l)
t := []string{"g", "h", "i"}
fmt.Println("dcl:", t)
twoD := make([][]int, 3)
for i := 0; i < 3; i++ {
innerLen := i + 1
twoD[i] = make([]int, innerLen)
for j := 0; j < innerLen; j++ {
twoD[i][j] = i + j
}
}
fmt.Println("2d: ", twoD)
}