混淆在Go中使用指针

Pat*_*dee 1 struct pointers go

我有下面的示例代码。

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"
    }
Run Code Online (Sandbox Code Playgroud)

问题是,为什么我必须在num变量之前放一个star(*)而不是a.Color?如果我这样做a.Color,它说

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

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

无效的运算:num + 2(类型不匹配的* int和int)

这让我感到困惑,有人可以解释为什么吗?

Him*_*shu 5

这是两种不同的情况:

情况1

num是一个指向int的指针,因此您需要将整数值添加到存储在指向的地址处的值上num。因此,您要取消引用num指针以获取存储在其中的值,如下所示:

func TestTest(num *int) {
    *num = *num + 2 // dereference the pointer to get the value.
}
Run Code Online (Sandbox Code Playgroud)

案例2

您正在将字符串值分配给Apple结构的Color字段,而不是指针。但是您使用的是指向结构的指针,而不是指向字段的指针。这就是为什么您可以分配这样的值的原因:

func Eat(a *Apple) { // a is the pointer to struct.
    a.Color = "green"
}
Run Code Online (Sandbox Code Playgroud)

现在,如果要生成与第一种情况相同的错误,请在结构内部创建一个指针类型Color域,如下所示:

type Apple struct {
    Color *string // this is the pointer value which will throw the same error in your current implementation.
}
Run Code Online (Sandbox Code Playgroud)

尝试在使用struct时将指针类型值分配给非指针变量时,Go操场上的错误代码。

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

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)
}
Run Code Online (Sandbox Code Playgroud)

操场上的例子

需要注意的另一件事是反射的值实际上是,reflect.Ptr因此我们可以做的是我们可以遍历struct字段来获取值,然后使用它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)))
}
Run Code Online (Sandbox Code Playgroud)


tex*_*uce 5

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

您还可以通过以下方式手动进行操作:

(*a).Color = "green"
Run Code Online (Sandbox Code Playgroud)

您的代码失败,可​​能是因为您未使用括号,而是试图取消引用Color属性而不是其自身。