3 ms·
foo is receiving a mutable reference and it can't modify the map without those changes leaking out permanently to the caller: https://go.dev/play/p/DXchC5Hq8o8
by erik_seaberg 2y ago
foo is receiving a mutable reference and it can't modify the map without those changes leaking out permanently to the caller: https://go.dev/play/p/DXchC5Hq8o8 https://go.dev/play/p/DXchC5Hq8o8. Passing maps by value would have prevented this by copying the contents.
It's a quirk of C++ that reference args can't be replaced but pointer args can.
- tsimionescu 2y agoThe point is that the local variable referencing the map is a different entity than the map itself. Foo gets a copy of that local variable, and the copy references the same map object. And the fact that C++ references can be used for assignment is essentially their whole point, not "a quirk".