package main
import (
"fmt"
"reflect"
)
type Config struct {
App *AppConfig
}
type AppConfig struct {
Name string
}
func (a *AppConfig) Parse() {
a.Name = "111"
}
var (
config = &Config{
App: &AppConfig{},
}
)
func main() {
v := reflect.ValueOf(*config)
typeOf := reflect.TypeOf(*config)
for i := 0; i < typeOf.NumField(); i++ {
method := v.Field(i).MethodByName("Parse")
method.Call([]reflect.Value{})
}
fmt.Println(config)
}
look at this, it can run successfully
but when i change
var (
config = &Config{
App: &AppConfig{},
}
)
to
var (
config = &Config{}
)
it will be failed with error #panic: runtime error: invalid memory address or nil pointer dereference#
how can i run successfully with
var (
config = &Config{}
)
Given
f := v.Field(i)wherefrepresents a pointer type, you first need to check whether the value represented byfisn'tnil, there's a method for that in the reflect package. And if you get backtrue, i.e. it isnil, then, before you can call a method on that field, you need to initialize that field so that is it notnilanymore. i.e.f.Set(reflect.New(f.Type().Elem())).And you also need to make sure that the
fis addressable, to do that, pass a pointer to the config to reflect, i.e.*Config, notConfig.https://go.dev/play/p/DMRFS41NXFS