Introduction
In this article we will talk about:
Maps
- What are they?
- How to creat a map?
- How to manipulate a map?
Maps
1. What are they?
Map is one of the collection types in Go. In addition to Slice/ Array, map has another dimention called key.
It works like a real world dictionary.
package main
import (
"fmt"
)
func main() {
statePops := map[string]int{
"California": 39250017,
"Texas": 27862596,
"Florida": 20612439,
}
fmt.Println(statePops)
}
// output
map[California:39250017 Florida:20612439 Texas:27862596]
Most of types in Go can be key of map, except Slices/ Maps/ Functions.
2. How to create a map?
a) Literal syntax
We can create a map by literal syntax(like above example). We just write out everything we need.
If we want to create an empty map, we can literal write: statepops := map[string]int{}
b) by make() function
We can create a slice by make() function. We can also create a map by make() function.
Especially when a map is key-value is later create in a for-loop, and we don‘t want to initialize a map at it‘s creation.
Not like in creating a Slice we have three arguments in make() function: length and capacity.
We only use one argument in creating a map. And the addinonal argument seems not working here.
package main
import (
"fmt"
)
func main() {
statePops := make(map[string]int, 2)
fmt.Println(statePops, len(statePops))
statePops["California"] = 39250017
fmt.Println(statePops, len(statePops))
statePops["Texas"] = 27862596
fmt.Println(statePops, len(statePops))
statePops["Florida"] = 20612439
fmt.Println(statePops, len(statePops))
}
// output
map[] 0
map[California:39250017] 1
map[California:39250017 Texas:27862596] 2
map[California:39250017 Florida:20612439 Texas:27862596] 3
3. How to manipulate a map?
a) Pull out a value: by brackets and it‘s key
For example, in first map example, we can use: statePops["California"]
b) Add an element to a map: by brackets and it‘s key
in first map example, we can use: statePops["Ohio"] = 11614373
c) Element‘s order: not guarantee
Even if in difiniton we write one elemnt before another, their order is not guarantee.
d) Delete an element: use build-in function delete()
This function has no return value. We will use it standalone.
package main
import (
"fmt"
)
func main() {
statePops := map[string]int{
"California": 39250017,
"Texas": 27862596,
"Florida": 20612439,
}
fmt.Println(statePops)
delete(statePops, "Texas")
fmt.Println(statePops)
}
// output
map[California:39250017 Florida:20612439 Texas:27862596]
map[California:39250017 Florida:20612439]
e) If a key doesn‘t exist: will return zero
Actually map will return two values: value of the key you appointed and bool of the key‘s exist.
package main
import (
"fmt"
)
func main() {
statePops := map[string]int{
"California": 39250017,
"Texas": 27862596,
"Florida": 20612439,
}
fmt.Println(statePops["Ohio"])
val, ok := statePops["Ohio"]
fmt.Println(val, ok)
}
f) Equal operation between two maps: share same memory address
If we manipulate one map, the result will also reflect on other one.
package main
import (
"fmt"
)
func main() {
statePops := map[string]int{
"California": 39250017,
"Texas": 27862596,
"Florida": 20612439,
}
sp := statePops
delete(sp, "Texas")
fmt.Println(sp)
fmt.Println(statePops)
}
// output
map[California:39250017 Florida:20612439]
map[California:39250017 Florida:20612439]
Maps in Go