Junior — Middle
What is considered the default value for elements in a map data structure?
sobes.tech AI
Answer from AI
In Go, if you access a key in a map that does not exist, it returns the zero value for the map's value type. For example, if the map has the type map[string]int, then for a missing key, it will return 0 — the zero value for int. For map[string]string, it will be an empty string "", and for map[string]*MyStruct, it will be nil.
Example:
m := make(map[string]int)
fmt.Println(m["not_exist"]) // Will print 0
Thus, the default value depends on the value type in the map and corresponds to the zero value of that type.