gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 02:29:40 31 4
gpt4 key购买 nike

我有 Go 函数,我想取消引用第一个值以存储在指针中。

例如:

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

这可能吗,还是我必须复制(在我的情况下非常大)返回值?

最佳答案

你不能那样做。地址运算符 & 不能应用于函数调用。

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.

如果确实重要的话,您应该做的是更改您的函数以首先返回一个指针。

关于pointers - 取消引用 go 中的第一个返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36984376/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com