main
kzkzzzz 2024-06-23 11:32:45 +08:00
parent a5fe1f32db
commit 26bf8320e3
2 changed files with 6 additions and 7 deletions

View File

@ -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))
}
}

View File

@ -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)
}