go - 带有数据列表的 yaml 文件
问题描述
全部,
我确信这很容易,但有点挣扎——尝试使用 gopkg.in/yaml.v3 编写一个将在 Go 程序中使用的 yaml 文件。我需要定义服务器列表及其相关元数据。在 JSON 中这是一个相当简单的过程,它是如何在 yaml 文件中处理的。
Go 代码结构如下。
type Config struct {
Servers struct {
Servers struct {
ServerType string `yaml:"serverType"`
ServerPort int `yaml:"serverPort"`
Auth struct {
AuthType string `yaml:"auth"`
TLSKey string `yaml:"tls"`
} `yaml:"auth"`
}`yaml:"server"`
} `yaml:"Servers"`
}
yaml 文件如下所示
Servers:
server:
serverType: production
serverPort: 80
auth:
auth: no
tls:
server:
serverType: test
serverPort: 8080
auth:
auth: no
tls:
我确信我错过了一些相当明显的东西 - 有什么智慧的话可以帮助我前进吗?
谢谢
解决方案
看起来您想要一组服务器。您不能在一个对象下重复相同的键:
Servers:
- serverType: production
...
- serverType: test
然后更改结构以匹配此:
type Config struct {
Servers []struct {
ServerType string `yaml:"serverType"`
ServerPort int `yaml:"serverPort"`
Auth struct {
AuthType string `yaml:"auth"`
TLSKey string `yaml:"tls"`
} `yaml:"auth"`
} `yaml:"Servers"`
}
推荐阅读
- c# - 使用 TCP 协议 C# 创建 API
- java - Java“this”-在 Eclipse 内容辅助中标记为静态的关键字
- python - 无法让“while True”条件发挥作用
- angular - Ionic Webview 在首次启动 IOS 时不同步 cookie
- arm - 如果使用了 THUMB,则用于 ARM 目标的 glibc 交叉编译在链接时失败
- html - CSS静态页脚和div浮动问题
- c++ - 如何在 C++ 中将文本文件读入向量?
- python - 根据Python中的最后一列查找未排序二维数组的列均值
- javascript - 如何操作 WAAPI 动画对象以循环播放动画时间轴中的特定点
- python - 带有全局变量的while和for循环不起作用Python已更新