diff --git a/myconf/conf.go b/myconf/conf.go index f217400..9a12b11 100644 --- a/myconf/conf.go +++ b/myconf/conf.go @@ -6,20 +6,19 @@ import ( ) var ( - vp *viper.Viper + vp = viper.New() ) -func Init(confPath string, conf interface{}) { - vp = viper.New() - vp.SetConfigFile(confPath) +func LoadFile(confFile string, conf interface{}) { + vp.SetConfigFile(confFile) err := vp.ReadInConfig() if err != nil { - panic(fmt.Errorf("读取配置文件失败 %s", err)) + panic(fmt.Errorf("read file fail: %s", err)) } err = vp.Unmarshal(conf) if err != nil { - panic(fmt.Errorf("解析配置文件失败 %s", err)) + panic(fmt.Errorf("parse file fail: %s", err)) } } diff --git a/myconf/conf_test.go b/myconf/conf_test.go index 3158e14..83ee6b5 100644 --- a/myconf/conf_test.go +++ b/myconf/conf_test.go @@ -20,6 +20,6 @@ func TestConf(t *testing.T) { } var config = &Config{} - Init("test.yaml", config) + LoadFile("test.yaml", config) fmt.Printf("%+v\n", config) }