简介
标准库reflect为Go语言提供了运行时动态获取对象的类型和值以及动态创建对象的能力。反射可以帮助抽象和简化代码,提高开发效率。
package mainimport ("fmt""os""reflect""strings")type Config struct {Name string `json:"server-name"`IP string `json:"server-ip"`URL string `json:"server-url"`Timeout string `json:"timeout"`}func readConfig() *Config {// read from xxx.json,省略config := Config{}typ := reflect.TypeOf(config)value := reflect.Indirect(reflect.ValueOf(&config))for i := 0; i < typ.NumField(); i++ {f := typ.Field(i)if v, ok := f.Tag.Lookup("json"); ok {key := fmt.Sprintf("CONFIG_%s", strings.ReplaceAll(strings.ToUpper(v), "-", "_"))if env, exist := os.LookupEnv(key); exist {value.FieldByName(f.Name).Set(reflect.ValueOf(env))}}}return &config}func main() {os.Setenv("CONFIG_SERVER_NAME", "global_server")os.Setenv("CONFIG_SERVER_IP", "10.0.0.1")os.Setenv("CONFIG_SERVER_URL", "geektutu.com")c := readConfig()fmt.Printf("%+v", c)}
