2016-05-02 14:03
浏览 63


I have Go function, and I want to dereference the first value to store in a pointer.


func foo() (int64, error) {...}
var A *int64
var err error
A, err = &foo()

Is this possible, or do I have to copy the (in my case very large) return value?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanping1920
    duanping1920 2016-05-02 14:11

    You can't do that. The address operator & cannot be applied on function calls.

    Spec: Address operators:

    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.

    What you should do is change your function to return a pointer in the first place, if it does matter.

    点赞 评论