yml文件解析
程序员文章站
2022-05-08 12:37:23
...
主要使用了开源组件github.com/uber-go/config
直接上实例吧,这里的环境变量及具体yml文件路径都是配置过的。
test.yml文件
yml:
desc: we are testing yml
代码package main
import (
"flag"
"fmt"
"github.com/uber-go/config"
"os"
"strings"
)
var (
configPath config.Provider
envName = flag.String("en", "MY_ENVIRMENT", "ENVIRONMENT name")
ymlPath = flag.String("config", "./config/test.yml", "config path")
)
// 加载环境变量,配置文件
func initConfig() {
flag.Parse()
Env := os.Getenv(*envName)
Env = strings.ToUpper(Env)
fmt.Println("env:", Env)
if Env != "DEV" {
panic("invalid " + *envName + " " + Env)
}
configPathList := strings.Split(*ymlPath, ";")
configPath = config.NewYAMLProviderFromFiles(configPathList...)
fmt.Println("conf:", configPath.Get("yml").Get("desc"))
}
func main() {
fmt.Println("test yml...")
initConfig()
}
运行结果:
test yml...
env: DEV
conf: we are testing yml
Process finished with exit code 0
env: DEV
conf: we are testing yml
Process finished with exit code 0
下一篇: yml为属性赋值