dongwan5381
2017-05-05 18:02
采纳率: 100%
浏览 191
已采纳

嵌套列表的解组YAML映射

I'm trying to write YAML for a datastructure which is both an int and a list of strings. But I'm having trouble getting the data structure and the YAML string to match. eg

package main

import (
    "fmt"
    "log"

    yaml "gopkg.in/yaml.v2"
)

type ThingAndGroups struct {
    Groups []string
    Value  int
}

var someStr = `
thing1:
  Groups:
    - g1
    - g2
  Value:
    5
`

func main() {
    t := make(map[string]ThingAndGroups)

    err := yaml.Unmarshal([]byte(someStr), &t)
    if err != nil {
        log.Fatalf("error: %v", err)
    }
    fmt.Printf("--- t:
%v

", t)
}

Returns

map[thing1:{[] 0}]

How do I get thing1 to be a list of strings?

1条回答 默认 最新

相关推荐 更多相似问题