Essential Go Basic types  Edit on GitHub      File Issue

Maps

A map is a mapping from a value of one type to a value of another type.

Other languages call them dictionaries (Python) or hash tables (C++).

Map basics:

m := make(map[string]int)
m["number3"] = 3
k := "number3"
if n, ok := m[k]; ok {
	fmt.Printf("value of %s is %d\n", k, n)
} else {
	fmt.Printf("key '%s' doesn't exist in map m\n", k)
}
k = "number4"
if n, ok := m[k]; ok {
	fmt.Printf("value of %s is %d\n", k, n)
} else {
	fmt.Printf("key '%s' doesn't exist in map m\n", k)
}
value of number3 is 3
key 'number4' doesn't exist in map m

Zero value of map is nil.

Learn more about maps.

  ↑ ↓ to navigate     ↵ to select     Esc to close