Maps
suggest changeMap is an unordered mapping from a key to a value.
Other languages call them dictionaries (Python) or hash tables (C++).
Zero value of map is nil.
Learn more about maps.
Map basics
m := make(map[string]int)
m["number3"] = 3
checkKey := func(k string) {
if n, ok := m[k]; ok {
fmt.Printf("value for key '%s' is %d\n", k, n)
} else {
fmt.Printf("key '%s' doesn't exist in map\n", k)
}
}
// get value for a key that exists
checkKey("number3")
// get value for a key that doesn't exist
checkKey("number4")
// remove a key
delete(m, "number3")
fmt.Printf("deleted key 'number3\n")
checkKey("number3") // and now it doesn't exist
value for key 'number3' is 3
key 'number4' doesn't exist in map
deleted key 'number3
key 'number3' doesn't exist in map
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents