Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
指向go中表达式结果的指针_Go - Fatal编程技术网

指向go中表达式结果的指针

指向go中表达式结果的指针,go,Go,我可以创建一个指向表达式结果的指针而不在Go中创建新变量吗 package test func foo(*uint32) { } func main() { foo(&(uint32(time.Now().Unix()))) //this line gives me error //cannot take the address of uint32(... } 在问题注释中,OP说protobuf消息需要指针 该包提供帮助函数,用于创建指向整数和其他值的指针 使用

我可以创建一个指向表达式结果的指针而不在Go中创建新变量吗

package test

func foo(*uint32) {

}

func main() {
    foo(&(uint32(time.Now().Unix()))) //this line gives me error
    //cannot take the address of uint32(...
}

在问题注释中,OP说protobuf消息需要指针

该包提供帮助函数,用于创建指向整数和其他值的指针

使用函数获取指向uint32的指针

x.Time = proto.Uint32(u)

为什么可以获取未声明变量的地址?如果它不存在于内存中,则无法获取地址。。int32值0不可寻址。谢谢!但是,还有其他标准方法可以创建指针吗?正如我所看到的,这很好用。。。func getAddress(s int32)*int32{return&s}@VitaliyAndrianovin在这种特殊情况下,您可以使用
new(int32)
创建一个指向
int32
的指针,其值为
0
。如果您需要一个指向非零值的指针,则需要一个变量声明。然后可以传递一个指向变量的指针。(您不能将指针传递给常量)按照您的建议分配给变量或在
getAddress
helper中写入一个变量。你为什么需要这个地址?也许有一个更高层次的问题可以在不获取函数返回地址的情况下解决。