pointers - 我如何在 Go 中做一个文字 *int64?

标签 pointers go struct literals

我有一个带有 *int64 的结构类型 field 。

type SomeType struct {
    SomeField *int64
}

在我的代码中的某个时候,我想声明一个文字(比如,当我知道所说的值应该是 0,或者指向 0 时,你知道我的意思)
instance := SomeType{
    SomeField: &0,
}

...除了这不起作用
./main.go:xx: cannot use &0 (type *int) as type *int64 in field value

所以我试试这个
instance := SomeType{
    SomeField: &int64(0),
}

...但这也不起作用
./main.go:xx: cannot take the address of int64(0)

我该怎么做呢?我能想出的唯一解决方案是使用占位符变量
var placeholder int64
placeholder = 0

instance := SomeType{
    SomeField: &placeholder,
}

注:&0当它是 *int 而不是 *int64 时,语法工作正常.编辑:不,它没有。为此表示歉意。

编辑:

显然,我的问题含糊不清。我正在寻找一种方法来真正说明 *int64 .这可以在构造函数中使用,或者用于说明文字结构值,甚至可以作为其他函数的参数。但是辅助函数或使用不同类型不是我正在寻找的解决方案。

最佳答案

Go 语言规范 ( Address operators ) 不允许采用数字常量的地址(不是无类型常量也不是类型常量的地址)。

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 [in the expression of &x] may also be a (possibly parenthesized) composite literal.


有关为什么不允许这样做的原因,请参阅相关问题:Find address of constant in go .一个类似的问题(同样不允许取其地址):How can I store reference to the result of an operation in Go?
您的选择(在 Go Playground 上尝试所有选项):
1) 与 new()您可以简单地使用内置 new() 函数来分配一个新的零值 int64并获取其地址:
instance := SomeType{
    SomeField: new(int64),
}
但请注意,这只能用于分配和获取指向任何类型的零值的指针。
2) 带辅助变量
对于非零元素,最简单和推荐的是使用一个可以获取地址的辅助变量:
helper := int64(2)
instance2 := SomeType{
    SomeField: &helper,
}
3) 具有辅助功能
注:我的 github.com/icza/gox 中提供了获取指向非零值的指针的辅助函数。图书馆,在 gox 包,因此您不必将它们添加到您需要的所有项目中。
或者,如果您多次需要此功能,您可以创建一个辅助函数来分配并返回 *int64 :
func create(x int64) *int64 {
    return &x
}
并使用它:
instance3 := SomeType{
    SomeField: create(3),
}
请注意,我们实际上没有分配任何东西,Go 编译器在我们返回函数参数的地址时这样做了。 Go 编译器执行转义分析,并在堆(而不是堆栈)上分配可能对函数进行转义的局部变量。详情见Is returning a slice of a local array in a Go function safe?
4) 带单行匿名函数
instance4 := SomeType{
    SomeField: func() *int64 { i := int64(4); return &i }(),
}
或者作为(较短的)替代方案:
instance4 := SomeType{
    SomeField: func(i int64) *int64 { return &i }(4),
}
5) 使用 slice 字面量、索引和取地址
如果您想要 *SomeField不包括 0 ,那么你需要一些可寻址的东西。
你仍然可以这样做,但这很丑陋:
instance5 := SomeType{
    SomeField: &[]int64{5}[0],
}
fmt.Println(*instance2.SomeField) // Prints 5
这里发生的是 []int64 slice 是用文字创建的,有一个元素 ( 5 )。并且它被索引(第0个元素)并取第0个元素的地址。在后台 [1]int64 的数组也将被分配并用作 slice 的支持数组。所以这里有很多样板。
6) 使用辅助结构文字
让我们检查可寻址性要求的异常(exception)情况:

As an exception to the addressability requirement, x [in the expression of &x] may also be a (possibly parenthesized) composite literal.


这意味着取复合文字的地址,例如结构文字没问题。如果我们这样做,我们将分配结构值并获得指向它的指针。但如果是这样,我们将可以使用另一个要求: “可寻址结构操作数的字段选择器” .因此,如果结构体字面量包含 int64 类型的字段,我们也可以取那个字段的地址!
让我们看看这个选项的实际效果。我们将使用这个包装结构类型:
type intwrapper struct {
    x int64
}
现在我们可以这样做:
instance6 := SomeType{
    SomeField: &(&intwrapper{6}).x,
}
请注意,这
&(&intwrapper{6}).x
意思如下:
& ( (&intwrapper{6}).x )
但是我们可以省略“外”括号作为地址运算符 &应用于 selector expression 的结果.
另请注意,在后台会发生以下情况(这也是有效的语法):
&(*(&intwrapper{6})).x
7) 带有辅助匿名结构体字面量
原理与案例#6 相同,但我们也可以使用匿名结构体字面量,因此不需要帮助器/包装器结构类型定义:
instance7 := SomeType{
    SomeField: &(&struct{ x int64 }{7}).x,
}

关于pointers - 我如何在 Go 中做一个文字 *int64?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34479891/

相关文章:

c - BGLIB SiliconLabs 代码错误?

C++ 使用 char* 复制字符数组(无字符串库)

http - Golang,零指针取消引用或无效内存地址

operators - 运算符 '<<' 和 '>>' 有什么作用?

c++ - 如何在不传递类实例的情况下从静态成员函数调用非静态成员函数

C++ 通过指针访问私有(private)函数

html - Go默认servemux异常消失的怪异错误

inheritance - 通过 Go 中的接口(interface)修改结构成员

c - 为保存指向不同结构的指针数组的结构填充数据

c - 为什么会出现段错误(核心已转储)?