Pointers 围棋中使用指针的困惑

Pointers 围棋中使用指针的困惑,pointers,go,struct,Pointers,Go,Struct,我的示例代码如下所示 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.Colo

我的示例代码如下所示

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.颜色的间接属性无效(类型字符串)

或者如果我从
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 struct的颜色字段指定一个字符串值,该字段不是指针。但您使用的是指向结构的指针,而不是指向字段的指针。这就是为什么您可以这样分配值:

func Eat(a *Apple) { // a is the pointer to struct.
    a.Color = "green"
}
现在,如果要生成与第一种情况相同的错误,请在结构内部创建指针类型颜色字段,如下所示:

type Apple struct {
    Color *string // this is the pointer value which will throw the same error in your current implementation.
}
使用struct时,尝试将指针类型值分配给非指针变量时出现错误代码

解决方案

要在结构中使用指针字段的情况下设置值,请将反射用作:

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)
}
范例

还要注意的是,反射的值实际上是
reflect.Ptr
,因此我们可以在结构字段上循环以获得值,然后使用
reflect.Indirect
来获得指针类型颜色字段的值

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)))
}
func-Eat(一个苹果){
str:=“绿色”
r:=反映(a).Elem()的.ValueOf
elm:=r.FieldByName(“颜色”)
elm.Set(反射值(&str))
fmt.Printf(“%+v\n”,(*a).Color)
对于i:=0;i
因为Go会自动在点属性之前取消对指针的引用

您也可以通过以下方式手动执行此操作:

(*a).Color = "green"
您的代码失败可能是因为您没有使用括号,并且它试图取消引用颜色属性而不是颜色属性本身