-
StackOverflow 文件
-
Go 教程
-
反射
-
結構
import "reflect"
type S struct {
A int
b string
}
func (s *S) String() { return s.b }
s := &S{
A: 5,
b: "example",
}
indirect := reflect.ValueOf(s) // effectively a pointer to an S
value := indirect.Elem() // this is addressable, since we've derefed a pointer
value.FieldByName("A").Interface() // 5
value.Field(2).Interface() // "example"
value.NumMethod() // 0, since String takes a pointer receiver
indirect.NumMethod() // 1
indirect.Method(0).Call([]reflect.Value{}) // "example"
indirect.MethodByName("String").Call([]reflect.Value{}) // "example"