3 ms·
The one thing that Go supports that I think no other GC language does is the ability to create a reference to a value at will. However, similar things can be d
by tsimionescu 4y ago
The one thing that Go supports that I think no other GC language does is the ability to create a reference to a value at will.
However, similar things can be done in C# for example by capturing a local variable in a closure, even if that variable is of a value type (struct). The closure then becomes something similar to a Go reference to that value, and you are still free to return it with no UB or other problems.
Either way, if your mental model is adjusted to the GC memory model, there is nothing special going on here. The weirdness comes only out of tying an object's lifetime to its scope. Scope is irrelevant to lifetime in GC languages, where lifetime is tied to (live) references to that object. So, if a reference to an object "escapes" its scope, that object will live beyond the scope where it was originally declared - nothing strange about that.