package setting import ( "log" "testing" "github.com/spf13/viper" ) type ConfigTest struct { HttpAddr string ` toml:"http_addr"` GrpcAddr string `json:"grpc_addr" toml:"grpcaddr" yaml:"grpc_addr"` JaegerUrl string `json:"jaeger_url" toml:"jaeger_url" yaml:"jaeger_url" mapstructure:"jaeger_url"` Tracing bool `toml:"tracing" json:"tracing" yaml:"tracing" ` // opentelemetry tracing } type ConfigTest2 struct { Host string ` toml:"Host"` Port string `json:"Port" toml:"Port" yaml:"Port"` Name string `json:"Name" toml:"Name" yaml:"Name" mapstructure:"Name"` Username string `toml:"Username" json:"Username" yaml:"Username" ` // opentelemetry tracing } type C1 struct { Abc ConfigTest2 `mapstructure:"database"` } // jaeger 加载配置文件 func TestSourceFile_Unmarshal(t *testing.T) { filePath := "config.toml" viper.SetConfigFile(filePath) if err := viper.ReadInConfig(); err != nil { t.Error(err) } c := &C1{} if err := viper.Unmarshal(&c); err != nil { t.Error(err) } log.Println("Unmarshal file sucess", "v", c) }