Golang YAML reading with map of maps

前端 未结 1 1104
时光说笑
时光说笑 2021-02-07 14:17

Here is my YAML file.

description: fruits are delicious
fruits:
  apple:
    - red
    - sweet
  lemon:
    - yellow
    - sour

I can read a fl

相关标签:
1条回答
  • 2021-02-07 15:02

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

    type Config struct {
      Description string
      Fruits map[string][]string
    }
    

    Printing the unmarshaled configuration with

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

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

    main.Config{Description:"fruits are delicious", 
         Fruits:map[string][]string{
              "lemon":[]string{"yellow", "sour"}, 
              "apple":[]string{"red", "sweet"}}}
    
    0 讨论(0)
提交回复
热议问题