大家好,我们又见面了啊~本文《如何将 yaml 解析/解组为数据结构》的内容中将会涉及到等等。如果你正在学习Golang相关知识,欢迎关注我,以后会给大家带来更多Golang相关文章,希望我们能一起进步!下面就开始本文的正式内容~
我有以下 yaml,我想将其解析为 go
env:
production:
asia:
blue: config.prod-asia
ph:
blue: prod.ph.config.blue
green: prod.phconfig.green
staging:
asia:
blue: asia.config.blue
ph:
blue: phconfig.blue
green: ph.config.green
但是我尝试了以下结构不起作用。
type env struct {
env map[string]region `yaml:"env"`
}
type region struct {
region map[string]config
}
type config map[string]string
我应该如何构建我的结构,以便可以解析为如下数据结构?
func main() {
var d env
source, err := ioutil.readfile("config.yaml")
if err != nil {
log.fatal("couldn't read yaml file.")
}
err = yaml.unmarshal(source, &d)
if err != nil {
log.fatal("couldn't parse yaml file.")
}
fmt.println(d)
}
上面运行时的输出是
931997371863yaml lib 使用了 "gopkg.in/yaml.v2"
解决方案
type env struct {
Env map[string]map[string]map[string]string `yaml:"env"`
}
用上面的例子解决了
今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~
本文内容来源于互联网,如有侵权请联系删除。
