|
|
|
|
|
by tsimionescu
680 days ago
|
|
No, there is a real difference, this is not splitting hairs. Go is always pass-by-value, even for maps [0]: x := map[int]int{1: 2}
foo(x)
fmt.Printf("%+v", x) //prints map[1:2]
func foo(a map[int]int) {
a = map[int]int{3: 4}
}
In contrast, C++ references have different semantics [1]: std::map<int, int> x {{1, 2}};
foo(x);
std::print("{%d:%d}", x.begin()->first, x.begin()->second);
//prints {3:4}
void foo(std::map<int, int>& a) {
a = std::map<int, int> {{3, 4}};
}
[0] https://go.dev/play/p/6a6Mz9KdFUh[1] https://onlinegdb.com/j0U2NYbjL |
|
It's a quirk of C++ that reference args can't be replaced but pointer args can.