Sobes.tech
Senior

Kus asub massiiv ja kaart — virna või virnas?

sobes.tech AI

Vastus AI-lt

In Go, arrays are usually allocated on the stack if their size is known at compile time and they are not too large. This is because arrays are fixed-size values. However, if an array is passed as a pointer or used in a structure allocated on the heap, then the array will also be on the heap.

In Go, a Map is a reference type, internally implemented via a pointer to a data structure on the heap. Therefore, a map is always located on the heap, even if the map variable is declared locally.

Example:

func example() {
    var arr [10]int // array on the stack
    m := make(map[int]int) // map on the heap
    _ = arr
    _ = m
}