使用reflect设置指针值?

时间:2016-08-12 12:12:23

标签: go reflection

我有这段代码,我想在其中设置结构内部指针的值。

package main

import (
    "fmt"
    "reflect"
)

type MyStruct struct {
    value *uint64 // value that I want to modify
}

func main() {
    var i uint64 = 1
    pointer := &MyStruct{&i}

    elem := reflect.ValueOf(pointer).Elem()
    field := elem.Field(0)
    fmt.Println(field.CanSet(), field.Elem().CanSet())
}

结果:

false false

不幸的是它不可设置。我该怎么办?

1 个答案:

答案 0 :(得分:2)

你的领域必须是公开的。将其命名为Value。

相关问题