在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
参考链接:https://studygolang.com/articles/26215?fr=sidebar viper 是一个配置解决方案,拥有丰富的特性:
demo: package main import ( "fmt" "log" "github.com/spf13/viper" ) func main() { viper.SetConfigName("config") viper.SetConfigType("toml") viper.AddConfigPath(".") viper.SetDefault("redis.port", 6381) err := viper.ReadInConfig() if err != nil { log.Fatal("read config failed: %v", err) } fmt.Println(viper.Get("app_name")) fmt.Println(viper.Get("log_level")) fmt.Println("mysql ip: ", viper.Get("mysql.ip")) fmt.Println("mysql port: ", viper.Get("mysql.port")) fmt.Println("mysql user: ", viper.Get("mysql.user")) fmt.Println("mysql password: ", viper.Get("mysql.password")) fmt.Println("mysql database: ", viper.Get("mysql.database")) fmt.Println("redis ip: ", viper.Get("redis.ip")) fmt.Println("redis port: ", viper.Get("redis.port")) } 配置文件:(toml格式) app_name = "awesome web" # possible values: DEBUG, INFO, WARNING, ERROR, FATAL log_level = "DEBUG" [mysql] ip = "127.0.0.1" port = 3306 user = "dj" password = 123456 database = "awesome" [redis] ip = "127.0.0.1" port = 7381
|
请发表评论