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

标签 pointers go dereference

我有 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/

相关文章:

C++ 访问冲突写入位置 0x0...设置 int *

go - Beego语法的功能 'Ctx.Input.GetData(' <variable-name >')'

c++ - 指针转换取消引用的指针有什么用?

C - 编译器错误 : dereferencing pointer to incomplete type

c - 堆上的多维数组 - C

c - 取消引用 ‘void *’ 指针和转换不起作用

go - Go 中的多态性——它存在吗?

go - 通过表单发送 ssh 私钥导致 "asn1: syntax error: data truncated"

c++ - "Iterator not dereferenceable"

C并发数组分配和释放