Junior — Middle
Millised andmetüübid sobivad Map-struktuuris võtmetena kasutamiseks?
sobes.tech AI
Vastus AI-lt
In Go, keys in a map structure can only be of types that support the equality comparison operation (== and !=). This is important because, to search for a value by key, the keys need to be compared.
Suitable key types:
- Simple types:
int,float64,string,bool, etc. - Pointers
- Structs, if all their fields are comparable
- Arrays, if their elements are comparable
Cannot be used as keys:
- Slices (
slice) - Maps (
map) - Functions
Example of usage:
m := make(map[string]int)
m["apple"] = 5
m["banana"] = 3
fmt.Println(m["apple"]) // 5
Trying to use a slice as a key will result in a compile-time error.