Sobes.tech
Junior — Middle

Mis on vaikimisi väärtus elementidele andmestruktuuris map?

sobes.tech AI

Vastus AI-lt

In Go, if you access a key in a map that does not exist, the default value for the map's value type is returned. For example, if the map has type map[string]int, then for a non-existent key, 0 is returned — the zero value for the int type. 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"]) // Prints 0

Therefore, the default value depends on the value type in the map and corresponds to the zero value of that type.