Marcel van Lohuizen | 07ee2ab | 2018-12-10 15:57:15 +0100 | [diff] [blame^] | 1 | package yaml_test |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "log" |
| 6 | |
| 7 | "gopkg.in/yaml.v2" |
| 8 | ) |
| 9 | |
| 10 | // An example showing how to unmarshal embedded |
| 11 | // structs from YAML. |
| 12 | |
| 13 | type StructA struct { |
| 14 | A string `yaml:"a"` |
| 15 | } |
| 16 | |
| 17 | type StructB struct { |
| 18 | // Embedded structs are not treated as embedded in YAML by default. To do that, |
| 19 | // add the ",inline" annotation below |
| 20 | StructA `yaml:",inline"` |
| 21 | B string `yaml:"b"` |
| 22 | } |
| 23 | |
| 24 | var data = ` |
| 25 | a: a string from struct A |
| 26 | b: a string from struct B |
| 27 | ` |
| 28 | |
| 29 | func ExampleUnmarshal_embedded() { |
| 30 | var b StructB |
| 31 | |
| 32 | err := yaml.Unmarshal([]byte(data), &b) |
| 33 | if err != nil { |
| 34 | log.Fatalf("cannot unmarshal data: %v", err) |
| 35 | } |
| 36 | fmt.Println(b.A) |
| 37 | fmt.Println(b.B) |
| 38 | // Output: |
| 39 | // a string from struct A |
| 40 | // a string from struct B |
| 41 | } |