Essential Go Maps  Suggest an edit

Zero value of a map

Zero value of a map is nil. It's length is 0.

var m map[string]string
fmt.Printf("m == nil ? %v\n", m == nil)
fmt.Printf("len(m) = %d\n", len(m))
m == nil ? true
len(m) = 0

Reading from a nil map behaves like reading from an empty map. Writing to a nil map crashes.

var m map[string]string

// you can read read from un-initialized map
fmt.Printf(`m["foo"] = %s`+"\n", m["foo"])
_, ok := m["foo"]
fmt.Printf("ok: %v\n", ok)

// writing to uninitialized map panics
m["foo"] = "bar"
m["foo"] = 
ok: false
panic: assignment to entry in nil map

goroutine 1 [running]:
	/private/var/folders/2k/p2_4052s70vd5_cfdm0k0l740000gn/T/src585861985/main.go:16 +0x189
exit status 2

  ↑ ↓ to navigate     ↵ to select     Esc to close