Sobes.tech
Junior — Middle

Kas massiive saab kasutada Map-kogumite võtmetena?

sobes.tech AI

Vastus AI-lt

In Go, keys in the map collection can only be types that support the comparison operation ==. Arrays in Go are comparable element by element, so fixed-length arrays can be used as keys.

Example:

package main
import "fmt"

func main() {
    m := make(map[[3]int]string)
    key := [3]int{1, 2, 3}
    m[key] = "example"
    fmt.Println(m[key]) // Will print: example
}

However, slices ([]int) cannot be used as keys because they are not comparable. If you need a collection with keys based on slices, you should convert them to a string or another comparable type.