【发布时间】:2020-06-05 11:52:09
【问题描述】:
输入 Yaml:
params:
- aws.region: ["us-west-1","us-west-2"]
- aws.s3path: ["s3-path-1", "s3-path-2"]
代码:
package main
import (
"fmt"
"io/ioutil"
"os"
"gopkg.in/yaml.v2"
)
func main() {
var f File
wfyaml, _ := os.Open("temp.yaml")
byteValue, _ := ioutil.ReadAll(wfyaml)
yaml.Unmarshal(byteValue, &f)
fmt.Print(f)
}
type File struct {
Params Params `yaml:"params"`
}
type Params []struct { // <<< This [] behavior is confusing
AwsRegion []string `yaml:"aws.region"`
S3path []string `yaml:"aws.s3path"`
}
输出:
{[{[us-west-1 us-west-2] []} {[] [s3-path-1 s3-path-2]}]}%
如果我使用
type Params struct { // <<< This [] behavior is confusing
AwsRegion []string `yaml:"aws.region"`
S3path []string `yaml:"aws.s3path"`
}
那么输出就是{{[] []}}%
我不确定我是否在这里遗漏了什么。
【问题讨论】:
-
你想达到什么输出? YAML 输入是否严格,即您不能更改其格式?或者您希望项目切片列表在一个结构中 - 而不是一个结构切片?