0

I don't get why the following code snippet isn't compiling. The compiler states:

cannot take the address of getAString()

The code:

func getAStringPointer() *string {
    return &getAString()
}

func getAString() string {
    return ""
}

But, storing the results of the function in auxliary variable and return the address of that variable the compiler behaves OK.

func getAStringPointer() *string {
    var aString = getAString()
    return &aString
}

func getAString() string {
    return ""
}
Victor
  • 3,841
  • 2
  • 37
  • 63
  • 2
    Try `&(getAString())`. – Kerrek SB Apr 07 '17 at 15:38
  • 1
    To comment on that. The order of operations means the `&` is applied before the function call `()`. The error you are getting is that you are trying to take the address of the funciton. – RayfenWindspear Apr 07 '17 at 15:40
  • @KerrekSB That doesn't work either (you could've tried). In Go you simply cannot take the address of return values of calls. – icza Apr 07 '17 at 16:14
  • @icza in the http://stackoverflow.com/questions/30744965/how-to-get-the-pointer-of-return-value-from-function-call the answer is quite long and doens't talk about the operator precedence as RayfanWindspear do. – Victor Apr 07 '17 at 16:19
  • @RayfenWindspear if what you are saying is true,that is the reason, and that was my question at first place... I'm not trying to archieve anything i just question why i couldn't make that assignment. Please izca re-read my question and the one you mark as duplicate. – Victor Apr 07 '17 at 16:21
  • @Victor It's not a matter of operator precedence, you get the same compiler error with or without using parenthesis. Try it. – icza Apr 07 '17 at 16:24
  • I guess i don't see what is the problem... if the operator precedence isn't the problem (@RayfenWindspear take note...) ... what is the problem? – Victor Apr 07 '17 at 16:27
  • @icza: I was too lazy to try, so I asked the OP to do it :-) Thanks for the clarification. (I confused this with taking the address of temporaries, which look a bit like a function call and which is something you can do.) – Kerrek SB Apr 07 '17 at 16:29
  • @Victor I think the error message is pretty clear. You used the `&` operator to take the address of something, something whose address cannot be taken by the language spec rules. Thus your code is invalid Go code, and attempting to compile it gives a compile-time error. – icza Apr 07 '17 at 16:31
  • Well I'll be.... – RayfenWindspear Apr 07 '17 at 16:32
  • @Victor For reasoning why this is not allowed, read my answer to a similar question: [How can I store reference to the result of an operation in Go?](http://stackoverflow.com/questions/34197248/how-can-i-store-reference-to-the-result-of-an-operation-in-go/34197367#34197367) – icza Apr 07 '17 at 16:35
  • thanks you very much @icza! I guess I'm being too much theoretical about operators and expressions... for a second i believe that "&" operator could be applied over ANY expression.. and would return the address of where a result (temporal or not) is stored. Thanks again. – Victor Apr 07 '17 at 16:42

1 Answers1

2

You can't apply & to a value like that (unless it's a composite literal). From the spec:

For an operand x of type T, the address operation &x generates a pointer of type *T to x. The operand must be addressable, that is, either a variable, pointer indirection, or slice indexing operation; or a field selector of an addressable struct operand; or an array indexing operation of an addressable array. As an exception to the addressability requirement, x may also be a (possibly parenthesized) composite literal. If the evaluation of x would cause a run-time panic, then the evaluation of &x does too.

Andy Schweig
  • 6,597
  • 2
  • 16
  • 22
  • ok @andy thanks!... besides the fact that an answer based on specification not fulfill my needed for logical reasoning about why i cant apply the & over an expression (that is after all what a function call is).. i would like to know what really happens.. – Victor Apr 07 '17 at 16:29
  • 1
    Applying the address operator to an expression would imply creating an anonymous variable to store the value and then taking the address of that. (You can only take the address of something that represents a value stored in memory). I can't think of any particular reason why that couldn't have been supported, but obviously the Go authors decided not to support it. Maybe someone else has a better explanation. You might want to try asking on https://groups.google.com/forum/#!forum/golang-nuts. – Andy Schweig Apr 07 '17 at 16:56
  • thanks andy! very useful comment, this along with the answer is acceptable as correct answer! – Victor Apr 07 '17 at 17:20