Go arrays and slices
Learn the difference between arrays and slices in Go, including declaration, slicing, the make function, and append operations.
This program demonstrates the key differences between arrays and slices in Go. Arrays have a fixed size defined at compile time, while slices are dynamically-sized views into underlying arrays. The example covers array declaration, slice operations, the make() function for pre-allocating capacity, and the append() function for growing slices.
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
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"fmt"
// "strconv"
// "math"
)
func arrays() {
grade1 := 97
grade2 := 85
grade3 := 93
grades := [3]int{11,22,33}
// [...]int{if I do not know how many elements will be stored in array}
gradesx := [...]int{11,22,33,444}
fmt.Printf("Grades: %v, %v, %v\n", grade1, grade2, grade3)
fmt.Printf("%v\n", grades)
fmt.Printf("%v\n", gradesx)
var students [3]string
fmt.Printf("Printing an empty array %v\n", students)
students[0] = "jano"
fmt.Printf("Printing filled array %v\n", students)
}
func slices() {
a := []int{1,4,7}
fmt.Printf("Printing slice %v\n", a)
fmt.Printf("Printing slice length: %v\n", len(a))
fmt.Printf("Printing slice capacity %v\n", cap(a))
x := []int{1,2,3,4,5,6,7,8,9,10}
b := x[:]
c := x[3:]
d := x[:6]
e := x[3:6]
// changes same underlying data ()
x[5] = 42
fmt.Printf("%v\n", x)
fmt.Printf("%v\n", b)
fmt.Printf("%v\n", c)
fmt.Printf("%v\n", d)
fmt.Printf("%v\n", e)
// special make() function
v := make([]int, 3, 100)
fmt.Printf("Printing slice %v\n", v)
fmt.Printf("Printing slice length: %v\n", len(v))
fmt.Printf("Printing slice capacity %v\n", cap(v))
j := []int{}
j = append(j, 10)
j = append(j, 20)
fmt.Printf("Printing slice %v\n", j)
fmt.Printf("Printing slice length: %v\n", len(j))
fmt.Printf("Printing slice capacity %v\n", cap(j))
t := []int{}
t = append(t, 88)
t = append(t, 77, 99)
//
t = append(t, []int{11, 22, 55}...)
fmt.Printf("Printing slice %v\n", t)
fmt.Printf("Printing slice length: %v\n", len(t))
fmt.Printf("Printing slice capacity %v\n", cap(t))
}
func main() {
// arrays()
slices()
}
This post is licensed under CC BY 4.0 by the author.