Junior — Middle
How does declaring a Map collection with the var keyword differ from initialization through assignment after declaration?
sobes.tech AI
Answer from AI
In Go, declaring a map collection with the var keyword creates a variable with a zero value, which is nil for a map that is not initialized and not ready for use. For example:
var m map[string]int
fmt.Println(m == nil) // true
To use such a map, it must be initialized, for example, with make or assignment:
m = make(map[string]int)
m["key"] = 1
If you declare and initialize a map immediately with an assignment, for example:
m := map[string]int{"key": 1}
then the map is ready for use with the specified elements.
Thus, var declares a variable with a zero value, and initialization with assignment immediately creates and fills the map.