2017-08-05 09:08:33 +00:00
|
|
|
package yaml_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2018-01-23 18:40:42 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
2017-08-05 09:08:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// An example showing how to unmarshal embedded
|
|
|
|
// structs from YAML.
|
|
|
|
|
|
|
|
type StructA struct {
|
|
|
|
A string `yaml:"a"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type StructB struct {
|
|
|
|
// Embedded structs are not treated as embedded in YAML by default. To do that,
|
|
|
|
// add the ",inline" annotation below
|
2018-01-23 18:40:42 +00:00
|
|
|
StructA `yaml:",inline"`
|
|
|
|
B string `yaml:"b"`
|
2017-08-05 09:08:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var data = `
|
|
|
|
a: a string from struct A
|
|
|
|
b: a string from struct B
|
|
|
|
`
|
|
|
|
|
|
|
|
func ExampleUnmarshal_embedded() {
|
|
|
|
var b StructB
|
|
|
|
|
|
|
|
err := yaml.Unmarshal([]byte(data), &b)
|
|
|
|
if err != nil {
|
2018-01-23 18:40:42 +00:00
|
|
|
log.Fatalf("cannot unmarshal data: %v", err)
|
2017-08-05 09:08:33 +00:00
|
|
|
}
|
2018-01-23 18:40:42 +00:00
|
|
|
fmt.Println(b.A)
|
|
|
|
fmt.Println(b.B)
|
|
|
|
// Output:
|
|
|
|
// a string from struct A
|
|
|
|
// a string from struct B
|
2017-08-05 09:08:33 +00:00
|
|
|
}
|