-j
Strictly speaking you can take the address of function's return value (eg. https://play.golang.org/p/0PTrkWEirW). It's like taking the address of any variable. But that's an lvalue, which &f() is not.
Conrad
Conrad
And I say this because in Go a function can have multiple return values, most commonly a (type, error) pair.
How should that work then? Add an exception so that single value return functions can be used like this? Add an exception so that when a couple of values are returned then if one of them is error it doesn't take the address for that? What would happen in case of more than two return arguments?
And there is also an exception for the counter rule: map elements are not addressable.
Please adapt it.
because of the automatic escape detection, I no longer think of a pointer as being the intrinsic address of a value; rather in my mind the & operator creates a new pointer value that when dereferenced returns the value.
With that mental model mixup in place, it's obvious why "&f()" makes sense — it's just creating a new pointer to the value returned by "f()".
If you instead keep in mind that the meaning of "&" is supposed to be closer to "what's the address of this thing?" for the purpose of identity-based equality and reference sharing, it makes more sense to prohibit "&m[k]" or "&f()" because each time you run those you may/will get a new pointer (which is not useful for identity-based equality or reference sharing). It still would be useful for my case which was essentially converting one type to an "optional" type, but maybe that's enough of an edge case that it doesn't matter.