Maps

In Go, maps are very similar to dictionaries in Python, or hashtables in Java.

Definition

The syntax to define a map is map[keyType]valueType {map block}, e.g.

1
2
3
4
5
6
m := map[string]string {
"name" : "Talk Golang",
"course" : "Golang",
"site" : "talkgolang.com",
"twitter" : "talkgolang"
}

The outputs of printing the above map is:

1
map[course:Golang name:Talk Golang site:talkgolang.com twitter:talkgolang]

The map definition can be nested, e.g. map[K1]map[K2]V, K1 is the key, and its value is another map.

Make a map

We could create a map with make method

1
2
3
m2 := make(map[string]int)

fmt.Println(m2)

m2 is an empty map.

1
map[]

We can also declare an empty map with var

1
2
3
var m3 map[string]int

fmt.Println(m3)
1
map[]

The difference is m2 is an empty map, but m3 is nil.

Traversing Map

Use for...range to traverse a map.

1
2
3
for k, v := range m{
fmt.Println("key =",k, "; val =", v, "; m[k] =", m[k])
}

The output will be:

1
2
3
4
key = name ; val = Talk Golang ; m[k] = Talk Golang
key = course ; val = Golang ; m[k] = Golang
key = site ; val = talkgolang.com ; m[k] = talkgolang.com
key = twitter ; val = talkgolang ; m[k] = talkgolang

Note, maps are unordered. The outputs of above traversing will change each time.

Accessing element

Similar to access an item in Python’s dictionary, we use key name to access an element in the map:

1
2
courseName := m["course"]
fmt.Println(courseName)

The outputs will be

1
Golang

However, the key name doesn’t exist in the map, Go will return a Zero value instead of raising errors like Python

1
2
causeName := m["cause"]
fmt.Println(causeName)

The outputs will be nil

1

Check if the key exists at all

We could check whether the target key exists or not in the map.

1
2
3
4
5
courseName, ok := m["course"]
fmt.Println(courseName, ok)

causeName, ok := m["cause"]
fmt.Println(causeName, ok)

The above will output:

1
2
Golang true
false

If the key doesn’t exist then ok is false. So we could refactor as such

1
2
3
4
5
if causeName, ok := m["cause"]; ok {
fmt.Println(causeName, ok)
} else {
panic("Key doesn't exist")
}

The output will be:

1
Key doesn't exist

Deleting Map Values

We could delete map values with delete method

1
2
3
4
5
6
7
8
name, ok := m["name"]
fmt.Println(name, ok)
delete(m, "name")
if name, ok := m["name"]; ok {
fmt.Println(name, ok)
} else {
fmt.Println("Key doesn't exist")
}

The outputs will be:

1
2
Talk Golang true
Key doesn't exist

More on the key

  • Go maps use hash tables, so the keys must be comparable
  • All built-in go data types can be used as map keys, except for slice, map, function

To Summarize

  • To create a map, we can use make method
  • To get a map value, map_name[key]
  • When key doesn’t exist, we will get a Zero value as its init value
  • Use value, ok := map_name[key] to check if key exists at all
  • Use delete(map_name, key) to delete the element
  • Use for...range to traverse the map, however traversing a map is unordered