【发布时间】:2020-12-16 02:31:39
【问题描述】:
我有一个如下所示的 YAML 配置文件
development: true
databases:
- label: masterdata
properties:
host: localhost
port: 54321
user: admin
password: nimda
dialect: postgres
dbname: business
- label: transactional
properties:
host: localhost
port: 54321
user: webuser
password: insecure
dialect: postgres
dbname: web
结构如下图
type ApplicationConfiguration struct {
development bool `yaml:"development"`
databases []Properties `yaml:"databases"`
}
type Properties struct {
Label string `yaml:"label"`
Values map[string]string `yaml:"properties"`
}
使用库gopkg.in/yaml.v2
尝试阅读本文
config := ApplicationConfiguration{}
b, _ := ioutil.ReadFile(configPath)
marshalError := yaml.Unmarshal(b, &config)
但config 中的数据库值始终为 nil,而 development 始终为 false,这意味着它根本没有被解组。如何阅读此配置,可能是我使用了错误的结构或 yaml 需要更改或不同的 API?
【问题讨论】:
-
这是因为
development和databases是私有的。yaml包只能看到公共字段。