@Dregorio: ja się zgodzę co do teorii, ale my mówimy o języku GO i w jego kontekście rozmawiamy, nawet linkowany przez Ciebie Dave Chaney, w artykule odnosi się do C++:
(...)
In languages** like C++** you can declare an alias, or an alternate name to an existing variable. This is called a reference variable.
(...)
Unlike C++, each variable defined in a Go program occupies a unique memory location.
Ale, żeby nie było, ja się z nim zgadzam:
(...)
A map value is a pointer to a runtime.hmap structure.
(...)
I tak pojmuje typy referencyjne w GO, zresztą nie tylko ja:
(...)
Map types are reference types, like pointers or slices, and so the value of m above is nil; it doesn't point to an initialized map.
(...)
Normalnie bym olal Twój post, ale polska sieć jest tak uboga w materiały o GO, że wprowadzanie takiego szumu uważam za szkodliwe. Dużo fajniej byłoby gdybyś sypnął przykładem np. takim:
package main
import (
"fmt"
)
func main() {
m := map[string]int{"originalMap": 1}
reallocate(m)
fmt.Println(m) // prints originalMap: 1
reallocatePtrWrong(&m)
fmt.Println(m) // prints originalMap: 1
reallocatePtr(&m)
fmt.Println(m) // prints overrideMap: 1
extend(m)
fmt.Println(m) // prints overrideMap: 1 extended: 2
}
func reallocate(m map[string]int) {
// Orginal value of m outside function not changed.
m = map[string]int{"overrideMap": 1}
}
func extend(m map[string]int) {
// Chaging m without realocation works, and don't need a pointer.
m["extended"] = 2
}
func reallocatePtrWrong(m *map[string]int) {
// Realocate using pointer don't become visible outisde if we
// only update the poniter value.
m = &map[string]int{"overrideMap": 1}
}
func reallocatePtr(m *map[string]int) {
// Realocate using pointer work if we do it right.
*m = map[string]int{"overrideMap": 1}
}
źródło: https://stackoverflow.com/questions/40680981/are-maps-passed-by-value-or-by-reference-in-go
Lub wyjaśnił, ża na początku mapy w GO były zapisywane w taki sposób: *map[int]int
, co nie wprowadzałoby niepotrzebnego zamieszania.
Natomiast dywagacje o "prawdziwych" referencjach pozostawił w komentarzach