Tutorial by Examples

You define a map using the keyword map, followed by the types of its keys and its values: // Keys are ints, values are ints. var m1 map[int]int // initialized to nil // Keys are strings, values are ints. var m2 map[string]int // initialized to nil Maps are reference types, and once defined ...
One can declare and initialize a map in a single statement using a composite literal. Using automatic type Short variable declaration: mapIntInt := map[int]int{10: 100, 20: 100, 30: 1000} mapIntString := map[int]string{10: "foo", 20: "bar", 30: "baz"} mapStringInt :...
The zero value of a map is nil and has a length of 0. var m map[string]string fmt.Println(m == nil) // true fmt.Println(len(m) ==0) // true A nil map has no keys nor can keys be added. A nil map behaves like an empty map if read from but causes a runtime panic if written to. var m map[string]...
import fmt people := map[string]int{ "john": 30, "jane": 29, "mark": 11, } for key, value := range people { fmt.Println("Name:", key, "Age:", value) } Note that when iterating over a map with a range loop, the iteration order i...
people := map[string]int{ "john": 30, "jane": 29, "mark": 11, } for key, _ := range people { fmt.Println("Name:", key) } If you are just looking for the keys, since they are the first value, you can simply drop the underscore: for key := r...
The delete built-in function removes the element with the specified key from a map. people := map[string]int{"john": 30, "jane": 29} fmt.Println(people) // map[john:30 jane:29] delete(people, "john") fmt.Println(people) // map[jane:29] If the map is nil or ther...
The built-in function len returns the number of elements in a map m := map[string]int{} len(m) // 0 m["foo"] = 1 len(m) // 1 If a variable points to a nil map, then len returns 0. var m map[string]int len(m) // 0
Maps in go are not safe for concurrency. You must take a lock to read and write on them if you will be accessing them concurrently. Usually the best option is to use sync.RWMutex because you can have read and write locks. However, a sync.Mutex could also be used. type RWMap struct { sync.RWMut...
m := make(map[string][]int) Accessing a non-existent key will return a nil slice as a value. Since nil slices act like zero length slices when used with append or other built-in functions you do not normally need to check to see if a key exists: // m["key1"] == nil && len(m[&qu...
To get a value from the map, you just have to do something like:00 value := mapName[ key ] If the map contains the key, it returns the corresponding value. If not, it returns zero-value of the map's value type (0 if map of int values, "" if map of string values...) m := map[string]s...
people := map[string]int{ "john": 30, "jane": 29, "mark": 11, } for _, value := range people { fmt.Println("Age:", value) } Note that when iterating over a map with a range loop, the iteration order is not specified and is not guaranteed to...
Like slices, maps hold references to an underlying data structure. So by assigning its value to another variable, only the reference will be passed. To copy the map, it is necessary to create another map and copy each value: // Create the original map originalMap := make(map[string]int) originalM...
Some languages have a native structure for sets. To make a set in Go, it's best practice to use a map from the value type of the set to an empty struct (map[Type]struct{}). For example, with strings: // To initialize a set of strings: greetings := map[string]struct{}{ "hi": {}, ...

Page 1 of 1