我有 Go 函数,我想取消引用第一个值以存储在指针中。
例如:
func foo() (int64, error) {...}
var A *int64
var err error
A, err = &foo()
这可能吗,还是我必须复制(在我的情况下非常大)返回值?
最佳答案
你不能那样做。地址运算符 &
不能应用于函数调用。
For an operand
x
of typeT
, the address operation&x
generates a pointer of type*T
tox
. 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 ofx
would cause a run-time panic, then the evaluation of&x
does too.
如果确实重要的话,您应该做的是更改您的函数以首先返回一个指针。
关于pointers - 取消引用 go 中的第一个返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36984376/