init
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/viper"
|
||||
"sundynix-go/config"
|
||||
)
|
||||
|
||||
func Viper() (cfg *config.Config) {
|
||||
viper.SetConfigFile("config.yaml")
|
||||
viper.SetConfigType("yaml")
|
||||
viper.AddConfigPath("../")
|
||||
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
||||
// Config file not found; ignore error if desired
|
||||
fmt.Println("Config file not found")
|
||||
} else {
|
||||
// Config file was found but another error was produced
|
||||
fmt.Println("Read config file error ")
|
||||
}
|
||||
}
|
||||
|
||||
//将所有值或特定值解组到结构、映射中
|
||||
//反序列化所有配置项
|
||||
if err := viper.Unmarshal(&cfg); err != nil {
|
||||
fmt.Printf("unmarshal error %s", err)
|
||||
return
|
||||
}
|
||||
return
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user