In go, the value of a map is the zero value when the key does not exist. I have a brief code snippet below: playground
package main
import (
"sync"
)
func main() {
var mm map[int]sync.Mutex
var m sync.Mutex
mm[1].Lock() // not work due to cannot call pointer method on mm[1] and cannot take the address of mm[1]
m.Lock() // work normal
}
What's the difference between mm[1]
and m
above? I used reflect to check, but cannot see the difference between them. Any clue about what cause the difference?