12

Assignment to entry in nil map

 3 years ago
source link: https://yourbasic.org/golang/gotcha-assignment-entry-nil-map/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Assignment to entry in nil map

yourbasic.org/golang
panic-face.jpg

Why does this program panic?

var m map[string]float64
m["pi"] = 3.1416
panic: assignment to entry in nil map

Answer

You have to initialize the map using the make function (or a map literal) before you can add any elements:

m := make(map[string]float64)
m["pi"] = 3.1416

See Maps explained for more about maps.

Share:             


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK