Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Arrays are defined by [int]type
- // Lengths are immutable!
- a := [3]int
- a[0] = 1
- a[1] = 2
- a[2] = 3
- a[3] = 4
- a[4] = 5
- // We can use literals
- a := [3]int{1, 2, 3}
- // Slices are pointers to array members
- a[2:4] // [3 4]
- a[:3] // [1 2 3]
- a[4:] // [5]
- // We can use make to initialize a slice with an implicit array
- a := make([]int, 5) // [0, 0, 0, 0, 0]
- cap(a) // 5
- len(a) // 5
- a := make([]int, 5, 10) // [0, 0, 0, 0, 0, nil, nil, nil, nil, nil]
- cap(a) // 10
- len(a) // 5
- // Slices can be appended to without worry about the immutable length
- a := make([]int, 5)
- a = append(a, 6) // [0 0 0 0 0 6]
- // Multidimensionality
- matrix := [][]int{
- []int{0, 0, 1},
- []int{0, 1, 0},
- []int{1, 0, 0},
- }
- matrix[2][2] = 10 // [[0 0 1][0 1 0][1 0 1]]
Add Comment
Please, Sign In to add comment