使用Golang读取包含嵌套映射的YAML文件。

huangapple go评论154阅读模式
英文:

Golang YAML reading with map of maps

问题

这是我的YAML文件。

  1. description: 水果很美味
  2. fruits:
  3. apple:
  4. - 红色
  5. - 甜的
  6. lemon:
  7. - 黄色
  8. - 酸的

我可以使用gopkg.in/yaml.v1包读取这个文件的扁平版本,但是我在尝试读取这个似乎有嵌套映射的YAML文件时遇到了困难。

  1. package main
  2. import (
  3. "fmt"
  4. "gopkg.in/yaml.v1"
  5. "io/ioutil"
  6. "path/filepath"
  7. )
  8. type Config struct {
  9. Description string
  10. Fruits []Fruit
  11. }
  12. type Fruit struct {
  13. Name string
  14. Properties []string
  15. }
  16. func main() {
  17. filename, _ := filepath.Abs("./file.yml")
  18. yamlFile, err := ioutil.ReadFile(filename)
  19. if err != nil {
  20. panic(err)
  21. }
  22. var config Config
  23. err = yaml.Unmarshal(yamlFile, &config)
  24. if err != nil {
  25. panic(err)
  26. }
  27. fmt.Printf("Value: %#v\n", config.Description)
  28. fmt.Printf("Value: %#v\n", config.Fruits)
  29. }

它无法获取嵌套的水果。似乎返回的是空的。Value: []main.Fruit(nil)

英文:

Here is my YAML file.

  1. description: fruits are delicious
  2. fruits:
  3. apple:
  4. - red
  5. - sweet
  6. lemon:
  7. - yellow
  8. - sour

I can read a flatter version of this with the gopkg.in/yaml.v1 package but I'm stuck trying to figure out how to read this YAML file when it's got what seems like a map of maps.

  1. package main
  2. import (
  3. "fmt"
  4. "gopkg.in/yaml.v1"
  5. "io/ioutil"
  6. "path/filepath"
  7. )
  8. type Config struct {
  9. Description string
  10. Fruits []Fruit
  11. }
  12. type Fruit struct {
  13. Name string
  14. Properties []string
  15. }
  16. func main() {
  17. filename, _ := filepath.Abs("./file.yml")
  18. yamlFile, err := ioutil.ReadFile(filename)
  19. if err != nil {
  20. panic(err)
  21. }
  22. var config Config
  23. err = yaml.Unmarshal(yamlFile, &config)
  24. if err != nil {
  25. panic(err)
  26. }
  27. fmt.Printf("Value: %#v\n", config.Description)
  28. fmt.Printf("Value: %#v\n", config.Fruits)
  29. }

It can't get the nested Fruits out. It seems to come back empty. Value: []main.Fruit(nil).

答案1

得分: 20

使用字符串切片的映射来表示水果属性:

  1. type Config struct {
  2. Description string
  3. Fruits map[string][]string
  4. }

使用以下代码打印解析后的配置:

  1. fmt.Printf("%#v\n", config)

产生以下输出(不包括我为了可读性而添加的空格):

  1. main.Config{Description:"水果很美味",
  2. Fruits:map[string][]string{
  3. "lemon":[]string{"黄色", "酸味"},
  4. "apple":[]string{"红色", "甜味"}}}
英文:

Use a map of string slices to represent the fruit properties:

  1. type Config struct {
  2. Description string
  3. Fruits map[string][]string
  4. }

Printing the unmarshaled configuration with

  1. fmt.Printf("%#v\n", config)

produces the following output (not including the whitespace I added for readability):

  1. main.Config{Description:"fruits are delicious",
  2. Fruits:map[string][]string{
  3. "lemon":[]string{"yellow", "sour"},
  4. "apple":[]string{"red", "sweet"}}}

huangapple
  • 本文由 发表于 2014年10月10日 09:20:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/26290485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定