Skip to content
Justice Tech School

Maps

Maps store key value pairs. Use the comma ok idiom to test a key and delete to remove.

Example
package main

import "fmt"

func main() {
	m := map[string]int{"a": 1}
	m["b"] = 2
	v, ok := m["z"]
	fmt.Println(m["a"], v, ok, len(m))
	delete(m, "a")
	fmt.Println(m)
}

Result

1 0 false 2
map[b:2]

Exercise

Test for a key.

v,  := m["k"]

Quiz Test

What is the zero value for a missing key?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.