我想定义默认定义字符串列表的YAML。我不希望这个字符串列表成为命名属性。我还想要一个可选的布尔参数。如在
package main
import (
"fmt"
"log"
yaml "gopkg.in/yaml.v2"
)
type ThingAndGroups struct {
Groups []string
boolval boolean
}
var someStr = `
thing1:
- g1
- g2
boolval:
y
thing2:
- g1
- g2
`
func main() {
t := make(map[string]ThingAndGroups)
err := yaml.Unmarshal([]byte(someStr), &t)
if err != nil {
log.Fatalf("error: %v", err)
}
fmt.Printf("--- t:\n%v\n\n", t)
}
然后返回
map [thing1:{[g1,g2] true},thing2:{[g1,g2] false}]
这可能吗?
我不想将YAML定义为
var someStr = `
thing1:
groups:
- g1
- g2
boolval:
y`
如果YAML没有boolval,我可以做到
func main() {
// NOTE THIS IS A MAP
t := make(map[string][]string)
err := yaml.Unmarshal([]byte(someStr), &t)
if err != nil {
log.Fatalf("error: %v", err)
}
fmt.Printf("--- t:\n%v\n\n", t)
}
答案 0 :(得分:0)
var someStr = `
thing1:
- g1
- g2
boolval:
y
thing2:
- g1
- g2
`
var someStr = `
thing1:
- g1
- g2
- boolval: y
thing2:
- g1
- g2
`