如何在Go中使用文字* int64?

问题描述 投票:70回答:2

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

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的方法。这可以在构造函数中使用,或者用于表示文字结构值,或者甚至作为其他函数的参数。但帮助函数或使用不同的类型不是我正在寻找的解决方案。

pointers go struct literals
2个回答
154
投票

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

操作数必须是可寻址的,即,变量,指针间接或切片索引操作;或可寻址结构操作数的字段选择器;或者可寻址数组的数组索引操作。作为可寻址性要求的一个例外,x [在&x的表达式]也可能是(可能是括号内的)composite literal

为了解释为什么不允许这样做,请参阅相关问题:Find address of constant in go。一个类似的问题(同样不允许采取其地址):How can I store reference to the result of an operation in Go?

你的选择(尝试所有在Go Playground):

1) With new()

你可以简单地使用内置的new()函数来分配一个新的零值int64并得到它的地址:

instance := SomeType{
    SomeField: new(int64),
}

但请注意,这只能用于分配和获取指向任何类型的零值的指针。

2) With helper variable

对于非零元素,最简单和建议使用可以采用其地址的辅助变量:

helper := int64(2)
instance2 := SomeType{
    SomeField: &helper,
}

3) With helper function

或者,如果您需要这么多次,您可以创建一个辅助函数,分配并返回一个*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) With a one-liner anonymous function

instance4 := SomeType{
    SomeField: func() *int64 { i := int64(4); return &i }(),
}

或者作为(较短的)替代方案:

instance4 := SomeType{
    SomeField: func(i int64) *int64 { return &i }(4),
}

5) With slice literal, indexing and taking address

如果你想要*SomeField不是0,那么你需要一些可寻址的东西。

你仍然可以这样做,但那很难看:

instance5 := SomeType{
    SomeField: &[]int64{5}[0],
}
fmt.Println(*instance2.SomeField) // Prints 5

这里发生的是一个[]int64切片是用文字创建的,有一个元素(5)。并且它被索引(第0个元素)并且取第0个元素的地址。在后台,还将分配[1]int64数组并将其用作切片的后备阵列。所以这里有很多样板。

6) With a helper struct literal

让我们检查可寻址性要求的例外情况:

作为可寻址性要求的一个例外,x [在&x的表达式]也可能是(可能是括号内的)composite literal

这意味着获取复合文字的地址,例如结构文字是可以的。如果我们这样做,我们将分配结构值并获得指针。但如果是这样,我们将可以使用另一个要求:“可寻址结构操作数的字段选择器”。因此,如果struct 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) With helper anonymous struct literal

原理与案例#6相同,但我们也可以使用匿名struct literal,因此不需要helper / wrapper struct type定义:

instance7 := SomeType{
    SomeField: &(&struct{ x int64 }{7}).x,
}

3
投票

使用返回int64变量地址的函数将解决问题。

在下面的代码中,我们使用函数f接受整数并返回一个保存整数地址的指针值。通过使用这种方法,我们可以轻松地解决上述问题。

 type myStr struct {
        url *int64
    }

    func main() {

        f := func(s int64) *int64 {
            return &s
        }
        myStr{
            url: f(12345),
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.