Slices and maps in Go
Learn Go · Beginner · 11 min · After Variables, types and constants in Go
How do you keep a list or a lookup table in Go?
1Guess
package main import "fmt" func main() { a := []int{1, 2, 3} b := a b[0] = 9 fmt.Println(a[0]) }2Read
Slices
A slice is a view on an array. It has a length,
len(s), and a capacity,cap(s).append(s, x)returns a slice that holds the extra element. It may use the same array or a new, larger one, so always keep the result.s[1:3]is a sub-slice: it shares the array withsand holds elements 1 and 2.3Code
Build a slice with append
1 / 2package main import "fmt" func main() { var s []int for i := 1; i <= 5; i++ { s = append(s, i*i) } fmt.Println(s, len(s)) fmt.Println(s[1:3]) for i, v := range s { fmt.Println(i, v) } }Read the program. What will it print?
4Read
Maps
A map holds key and value pairs. Write
m := map[string]int{"a": 1}ormake(map[string]int). Read withm["a"], write withm["a"] = 2and remove withdelete(m, "a"). A missing key gives the zero value of the value type. To tell a missing key from a stored zero, usev, ok := m["a"].5Code
Count words with a map
1 / 2package main import "fmt" func main() { words := []string{"go", "is", "fun", "go", "go", "is"} counts := make(map[string]int) for _, w := range words { counts[w]++ } fmt.Println(counts["go"], counts["is"], counts["fun"]) fmt.Println(counts["rust"]) n, ok := counts["rust"] fmt.Println(n, ok) delete(counts, "fun") fmt.Println(len(counts)) }Read the program. What will it print?
6Fix
package main import "fmt" func main() { var m map[string]int m["a"] = 1 fmt.Println(m["a"]) }7Say it back
8Mission · 10 min
Make a slice of five names, append a sixth, print its length, then count how often each first letter appears with a map and print the counts for two letters you pick.
go run .
Common questions
- What is a slice in Go?
- A slice is a descriptor of a segment of an array: it holds a pointer to the array, a length and a capacity. Slices are the usual way to keep a list in Go. Copying a slice variable copies only the descriptor, so both variables see the same elements. That is why a change through one slice can show up in another.
- How do I add an item to a slice in Go?
- Call append and assign the result back: s = append(s, x). The function appends the element to the end of the slice and grows the underlying array when it needs more room. Because append returns the slice it built, ignoring the result loses the new element. You can add several values at once by listing them after the slice.
- How do I check if a key exists in a Go map?
- Use the two-value form: v, ok := m[key]. The variable ok is true when the key is in the map and false when it is not, in which case v is the zero value of the value type. This tells a missing key apart from a stored zero, which a plain m[key] lookup cannot do.
Sources
- https://go.dev/blog/slices-intro (read 2026-12-03)
- https://go.dev/blog/maps (read 2026-12-03)
- https://go.dev/ref/spec#Appending_and_copying_slices (read 2026-12-03)
Measured with go1.27.4 darwin/arm64 on 2026-12-03
Published Dec 3, 2026 · Updated Dec 3, 2026