News for hackers. Stuff that matters.
Re:Go Is a Well-Designed Language by jitl on Friday January 31, 2025 @07:47AM (#42887132)
append returns a new pointer in case the slice moved, map assignment does not return a new pointer. There’s no way for the operator to change the reference you hold from a nil to a pointer to the newly allocated value. When memory is allocated under the hood on assignment to a non-nil map, your reference to the object remains the same, but some internal pointers change.

You can write a func Set[k, v](map map[k]v, k k, v v) map[k]v yourself that returns a map reference if you want this style of “maybe return a new pointer”.

There are no comments on this comment yet.