我有一个带*int64字段的结构类型.
type SomeType struct {
SomeField *int64
}
Run Code Online (Sandbox Code Playgroud)
在我的代码中的某个时刻,我想声明一个这样的文字(比如,当我知道所述值应为0,或者指向0时,你知道我的意思)
instance := SomeType{
SomeField: &0,
}
Run Code Online (Sandbox Code Playgroud)
...除了这不起作用
./main.go:xx: cannot use &0 (type *int) as type *int64 in field value
Run Code Online (Sandbox Code Playgroud)
所以我试试这个
instance := SomeType{
SomeField: &int64(0),
}
Run Code Online (Sandbox Code Playgroud)
......但这也行不通
./main.go:xx: cannot take the address of int64(0)
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?我能想出的唯一解决方案是使用占位符变量
var placeholder int64
placeholder = 0
instance := SomeType{
SomeField: &placeholder,
}
Run Code Online (Sandbox Code Playgroud)
注意:当它是*int而不是a时,编辑:不,不.为此表示歉意.&0语法工作正常*int64.
编辑:
显然,我的问题含糊不清.我正在寻找一种方法来说明一个*int64.这可以在构造函数中使用,或者用于表示文字结构值,或者甚至作为其他函数的参数.但帮助函数或使用不同的类型不是我正在寻找的解决方案.