Zero value of a map

suggest change

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]:
main.main()
	/tmp/src761892483/main.go:16 +0x1a5
exit status 2

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents