dpl74687 2016-05-02 14:03
浏览 64
已采纳

取消引用go中的第一个返回值

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

E.g.:

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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 fluent无法启动
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。