Here is the code, I expect it will panic when visiting mp["12"]
, but it works fine there
// You can edit this code!
// Click here and start typing.
package main
import "log"
func main() {
var mp map[string]int = nil
log.Println(mp["12"], "12") // works fine
if mp == nil {
panic("map is nil") // panic here
}
}
You can read from a nil map, but cannot write to it. The language spec says:
A nil map is equivalent to an empty map except that no elements may be added.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With