August 2019
Beginner to intermediate
798 pages
17h 2m
English
The following Go code will work:
aMap := map[string]int{}
aMap["test"] = 1
However, the next Go code will not work because you have assigned the nil value to the map you are trying to use:
aMap := map[string]int{}
// var aMap map[string]int
aMap = nil
fmt.Println(aMap)
aMap["test"] = 1
Saving the preceding code to failMap.go and trying to compile it will generate the next error message:
$ go run failMap.go map[] panic: assignment to entry in nil map ...
This means that trying to insert data to a nil map will fail. However, looking up, deleting, finding the length, and using range loops on nil maps will not crash your code.
Read now
Unlock full access