I have struct
type ChartOpts struct {
Name mypakage.MyType
Repo mypakage.MyType
}
on which I want to set a receiver for reflection.
func (chartOpts *ChartOpts) BindFlags() {
fields := reflect.TypeOf(chartOpts)
values := reflect.ValueOf(chartOpts)
num := fields.NumField()
fmt.Println(fields, values, num)
}
The above code panics
panic: reflect: NumField of non-struct type *app.ChartOpts
Why do I need to call the Elem()
method to fix this?
func (chartOpts *ChartOpts) BindFlags() {
fields := reflect.TypeOf(chartOpts)
values := reflect.ValueOf(chartOpts)
num := fields.Elem().NumField()
fmt.Println(fields, values, num)
}
Aucun commentaire:
Enregistrer un commentaire