在 go 中取消引用第一个 return 值

Dereferencing the first return value in go

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

例如:

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

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

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

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.

你应该做的是首先将函数更改为 return 一个指针,如果它确实重要的话。