-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (61 loc) · 1.54 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
package slices
import "fmt"
/*
Slices are much more common than arrays, they are typed by the elements they contain
and providing a fixed size is not a hard requirement.
An uninitialized slice equals to nil and has a 0 length.
Slices are built using the `make` keyword
*/
func Run() {
basicSlice()
indexability()
copySlice()
slicingASlice()
multiDimensionalSlice()
}
func basicSlice() {
// Instantiating a slice is slightly different than instantiating an array
var uninitialized []string
fmt.Println(uninitialized == nil) // true
fmt.Println(uninitialized) // nil, but prints as [].
// slices are made using the `make` keyword.
// Create a slize of length 3
sizeSlice := make([]string, 3)
fmt.Println(cap(sizeSlice), len(sizeSlice)) // 3, 3
for _, element := range sizeSlice {
// empty values are initialized.
fmt.Println(element) // " ", " ", " "
}
}
func indexability() {
mySlice := make([]int, 3)
mySlice[1] = 200
fmt.Println(mySlice) // [0, 200, 0]
}
func copySlice() {
initial := make([]int, 3)
initial[0] = 100
initial[1] = 200
initial[2] = 300
other := make([]int, 3)
copy(other, initial)
fmt.Println(other, initial)
other[2] = 1000
// deep copy, only other is changed.
fmt.Println(other, initial)
}
func slicingASlice() {
object := []int{1, 2, 3, 4, 5}
fmt.Println(object[:2]) // [1, 2]
}
func multiDimensionalSlice() {
twoDArray := make([][]int, 3)
for i := 0; i < 3; i++ {
inner := i + 1
twoDArray[i] = make([]int, inner)
for j := 0; j < inner; j++ {
twoDArray[i][j] = i + j
}
}
fmt.Println(twoDArray)
}