Golang:给结构体成员赋值,该成员是一个指针。

huangapple go评论76阅读模式
英文:

Golang: Assigning a value to struct member that is a pointer

问题

我正在尝试为一个指针类型的结构成员赋值,但在运行时出现了"panic: runtime error: invalid memory address or nil pointer dereference"的错误。

package main

import (
	"fmt"
	"strconv"
)

// Test
type stctTest struct {
	blTest *bool
}

func main() {

	var strctTest stctTest
	*strctTest.blTest = false

	fmt.Println("Test is " + strconv.FormatBool(*strctTest.blTest))

}

这个运行时错误似乎是由于*strctTest.blTest = false这行代码的赋值引起的,但是为什么会出错呢?我该如何将其设置为false呢?

英文:

I'm trying to assign a value to a struct member that is a pointer, but it gives "panic: runtime error: invalid memory address or nil pointer dereference" at runtime...

package main

import (
	"fmt"
	"strconv"
)

// Test
type stctTest struct {
	blTest *bool
}

func main() {

	var strctTest stctTest
	*strctTest.blTest = false

	fmt.Println("Test is " + strconv.FormatBool(*strctTest.blTest))

}

The runtime error seems to come from the assignment of the value with *strctTest.blTest = false , but why? How do I set it to false?

答案1

得分: 17

为什么会出错?因为指针只是指向一个位置,它本身并不创建任何要指向的内容。你需要自己创建。

如何将其设置为false?这取决于你为什么将其设置为指针。

如果每个副本都应该指向相同的布尔值,那么应该在创建函数中为其分配一些空间。

func NewStruct() *strctTest {
    bl := true
    return &strctTest{
        blTest: &bl,
    }
}

如果用户应该将其指向自己的布尔值,那么在创建对象时应该手动设置。

func main() {
    myBool := false
    stctTest := strctTest{
        blTest: &myBool,
    }

    fmt.Println("Test is " + strconv.FormatBool(*strctTest.blTest))
}
英文:

Why is it an error? Because a pointer only points. It doesn't create anything to point AT. You need to do that.

How to set it to false? This all depends on WHY you made it a pointer.

Is every copy of this supposed to point to the same bool? Then it should be allocated some space in a creation function.

func NewStruct() *strctTest {
    bl := true
    return &strctTest{
        blTest: &bl,
     }
}

Is the user supposed to point it at a boolean of his own? Then it should be set manually when creating the object.

func main() {
    myBool := false
    stctTest := strctTest{
        blTest: &myBool
    }

    fmt.Println("Test is " + strconv.FormatBool(*strctTest.blTest))

}

答案2

得分: 2

另一种思考方式是布尔类型的零值为false。

这个不太清楚,但还有另一种方法可以实现。

https://play.golang.org/p/REbnJumcFi

我建议使用一个New()函数,返回一个初始化的结构体类型的引用。

英文:

Another way you can think of it is the zero value of a boolean is false.

This is not as clear but another way to do it.

https://play.golang.org/p/REbnJumcFi

I would recommend a New() func that returns a reference to a initialized struct type.

答案3

得分: 2

你也可以这样做:

package main

import (
    "fmt"
    "strconv"
)

// 测试
type stctTest struct {
    blTest *bool
}

func main() {

    strctTest := stctTest{
        blTest: &[]bool{true}[0],
    }

    fmt.Println("测试结果为:" + strconv.FormatBool(*strctTest.blTest))

}

链接:https://play.golang.org/p/OWSosQhrUql

英文:

You could also do something like:

package main

import (
    "fmt"
    "strconv"
)

// Test
type stctTest struct {
    blTest *bool
}

func main() {

    strctTest := stctTest{
        blTest: &[]bool{true}[0],
    }

    fmt.Println("Test is " + strconv.FormatBool(*strctTest.blTest))

}

https://play.golang.org/p/OWSosQhrUql

答案4

得分: 0

以下是翻译好的内容:

根据JT的回答,我也建议使用new函数,如下所示:

package main

import (
    "fmt"
    "strconv"
)

// Test
type stctTest struct {
    blTest *bool
}

func main() {

    strctTest := &stctTest{
        blTest: new(bool),
    }

    *strctTest.blTest = true

    fmt.Println("Test is " + strconv.FormatBool(*strctTest.blTest))
}

在使用new()初始化内存之后,您可以直接给解引用的指针赋值。这样,您就不需要使用另一个变量来获取地址。

链接:https://go.dev/play/p/BmekoTalQVh

英文:

Following up on JTs Answer, I also would recommend using the new function as such:

package main

import (
    "fmt"
    "strconv"
)

// Test
type stctTest struct {
    blTest *bool
}

func main() {

    strctTest := &stctTest{
        blTest: new(bool),
    }

    *strctTest.blTest = true

    fmt.Println("Test is " + strconv.FormatBool(*strctTest.blTest))
}

After initializing the memory with new(), you can directly assign a value to the de-referenced pointer. This way you do not need to use another variable to get the address from.

https://go.dev/play/p/BmekoTalQVh

huangapple
  • 本文由 发表于 2017年5月6日 02:31:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/43811652.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定