Maps

Assigning to an unitialized map causes a panic


	var a map[string]int
	a["testKey"] = 1
panic: assignment to entry in nil map

goroutine 1 [running]:
main.main()
	/tmp/main.go:7 +0x73
exit status 2

Initialize an empty map and add keys


totalWins := map[string]int{}
totalWins["lakers"] = 20
totalWins["suns"]++
fmt.Println(totalWins)
map[lakers:20 suns:1]

Initialize with map literals


x := map[int]string{
	10: "wow cool",
	40: "I like it",
}
fmt.Println(x)
map[10:wow cool 40:I like it]

Multi level map literal


	teams := map[string][]string{
		"Orcas":   {"Fred", "Ralph", "Bijou"},
		"Lions":   {"Sarah", "Peter", "Billie"},
		"Kittens": {"Waldo", "Raul", "Ze"},
	}
	fmt.Println(teams)
map[Kittens:[Waldo Raul Ze] Lions:[Sarah Peter Billie] Orcas:[Fred Ralph Bijou]]

Check if the key exists and print it if it does


v, ok := teams["Orcas"]
if(ok) { 
	fmt.Println(v)
}
[Fred Ralph Bijou]

Delete a key


delete(teams, "Orcas")
fmt.Println(teams)
map[Kittens:[Waldo Raul Ze] Lions:[Sarah Peter Billie]]

Notes

  • Map keys can be any comparable type