Skip to content

Instantly share code, notes, and snippets.

@ksindi
Last active November 25, 2018 02:21
Show Gist options
  • Save ksindi/125c3495f33165571659277153bd4ee1 to your computer and use it in GitHub Desktop.
Save ksindi/125c3495f33165571659277153bd4ee1 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"fmt"
"gopkg.in/yaml.v2"
)
var data = `
a: Easy!
b:
c: 0
d: [1, 2]
---
a: Easy!
b:
c: 1000
d: [3, 4]
`
// Note: struct fields must be public in order for unmarshal to
// correctly populate the data.
type T struct {
A string
B struct {
RenamedC int `yaml:"c"`
D []int `yaml:",flow"`
}
}
func main() {
r := bytes.NewReader([]byte(data))
dec := yaml.NewDecoder(r)
var t T
for dec.Decode(&t) == nil {
fmt.Printf("a :%v\nb :%v\n", t.A, t.B)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment