登录
首页 >  Golang >  Go问答

Go 中使用指针的困惑

来源:stackoverflow

时间:2024-03-16 19:51:30 207浏览 收藏

在 Go 中使用指针时,您需要区分指向基本类型(如 int)的指针和指向结构体或其他复杂类型的指针。对于基本类型,您需要取消引用指针才能访问其值,而对于结构体,您可以直接访问其字段。例如,对于指向 int 的指针 num,您需要 *num 来获取其值,而对于指向 Apple 结构体的指针 a,您可以直接访问 a.Color。 如果您尝试对结构体的非指针字段使用指针语法(如 a.Color),则会收到编译错误。要修改结构体的指针字段,可以使用反射,或手动取消引用指针(如 (*a).Color)。

问题内容

我的示例代码如下。

type Apple struct {
    Color string
}
//In this way, the code runs just fine.
func main(){
        var test = 6
        TestTest(&test)
        fmt.Println(test)
        a := Apple{"red"}
        Eat(&a)
        fmt.Println(a.Color)
}


    func TestTest(num *int) {
        *num = *num + 2
    }
    func Eat(a *Apple) {
        a.Color = "green"
    }

问题是,为什么我必须在 num 变量之前放置一个星号(*),但对于 a.color 则不需要? 如果我对 a.color 这样做,它会说

a.color 的间接无效(字符串类型)

或者如果我从 num 中删除一个星号(*),它会说

无效操作:num + 2(*int 和 int 类型不匹配)

这让我很困惑,有人能解释一下为什么吗?


解决方案


这是两种不同的情况:

案例1

num 是一个指向 int 的指针,因此您需要将整数值与 num 指向的地址存储的值相加。因此,您取消引用 num 指针以获取存储在其中的值:

func testtest(num *int) {
    *num = *num + 2 // dereference the pointer to get the value.
}

案例2

您正在向 apple 结构的 color 字段分配一个字符串值,该值不是指针。但是您使用的是指向结构而不是字段的指针。这就是为什么您可以分配这样的值:

func eat(a *apple) { // a is the pointer to struct.
    a.color = "green"
}

现在,如果您想生成与第一种情况相同的错误,请在结构内创建一个指针类型 color 字段,如下所示:

type apple struct {
    color *string // this is the pointer value which will throw the same error in your current implementation.
}

在使用结构体时尝试将指针类型值分配给非指针变量时出现错误代码 Go playground

解决方案

要在结构中使用指针字段时设置值,请使用反射:

package main

import (
    "fmt"
    "reflect"
)

//i have my sample code like this.

type apple struct {
    color *string
}

//in this way, the code runs just fine.
func main() {
    var test = 6
    testtest(&test)
    fmt.println(test)
    point := "red"
    a := apple{&point}
    eat(&a)
    fmt.println(a.color)
}

func testtest(num *int) {
    *num = *num + 2
}
func eat(a *apple) {
    str := "green"
    r := reflect.valueof(a)
    elm := r.elem().fieldbyname("color")
    elm.set(reflect.valueof(&str))
    fmt.printf("%+v", (*a).color)
}

Playground 示例

还有一点需要注意的是,反射的值实际上是 reflect.ptr 所以我们可以做的是循环遍历结构体字段来获取值,然后使用 reflect.indirect 来获取指针类型 color 的值字段。

func eat(a *apple) {
    str := "green"
    r := reflect.valueof(a).elem()
    elm := r.fieldbyname("color")
    elm.set(reflect.valueof(&str))
    fmt.printf("%+v\n", (*a).color)
    for i := 0; i < r.numfield(); i++ {
        valuefield := r.field(i)
        fmt.println(reflect.indirect(valuefield))
    }
    // or use fieldbyname to get the value of a field.
    st := "color"
    fmt.println(reflect.indirect(reflect.valueof(a).elem().fieldbyname(st)))
}

因为 go 自动在点属性之前取消引用指针。

您也可以通过以下方式手动完成:

(*a).Color = "green"

您的代码失败可能是因为您没有使用括号,并且它尝试取消引用 color 属性而不是其本身。

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《Go 中使用指针的困惑》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>