| 1 | package yaml_testscip-go gomod go.yaml.in/yaml/v2 . `go.yaml.in/yaml/v2_test`/ |
| 2 | |
| 3 | import ( |
| 4 | "fmtscip-go gomod github.com/golang/go/src go1.15 fmt/" |
| 5 | "logscip-go gomod github.com/golang/go/src go1.15 log/" |
| 6 | |
| 7 | "go.yaml.in/yaml/v2scip-go gomod go.yaml.in/yaml/v2 . `go.yaml.in/yaml/v2`/" |
| 8 | ) |
| 9 | |
| 10 | // An example showing how to unmarshal embedded |
| 11 | // structs from YAML. |
| 12 | |
| 13 | type StructAscip-go gomod go.yaml.in/yaml/v2 . `go.yaml.in/yaml/v2_test`/StructA# struct { |
| 14 | Ascip-go gomod go.yaml.in/yaml/v2 . `go.yaml.in/yaml/v2_test`/StructA#A. string `yaml:"a"` |
| 15 | } |
| 16 | |
| 17 | type StructBscip-go gomod go.yaml.in/yaml/v2 . `go.yaml.in/yaml/v2_test`/StructB# struct { |
| 18 | // Embedded structs are not treated as embedded in YAML by default. To do that, |
| 19 | // add the ",inline" annotation below |
| 20 | StructAscip-go gomod go.yaml.in/yaml/v2 . `go.yaml.in/yaml/v2_test`/StructB#StructA. `yaml:",inline"` |
| 21 | Bscip-go gomod go.yaml.in/yaml/v2 . `go.yaml.in/yaml/v2_test`/StructB#B. string `yaml:"b"` |
| 22 | } |
| 23 | |
| 24 | var datascip-go gomod go.yaml.in/yaml/v2 . `go.yaml.in/yaml/v2_test`/data. = ` |
| 25 | a: a string from struct A |
| 26 | b: a string from struct B |
| 27 | ` |
| 28 | |
| 29 | func ExampleUnmarshal_embeddedscip-go gomod go.yaml.in/yaml/v2 . `go.yaml.in/yaml/v2_test`/ExampleUnmarshal_embedded().() { |
| 30 | var b StructBscip-go gomod go.yaml.in/yaml/v2 . `go.yaml.in/yaml/v2_test`/StructB# |
| 31 | |
| 32 | err := yamlscip-go gomod go.yaml.in/yaml/v2 . `go.yaml.in/yaml/v2`/.Unmarshalscip-go gomod go.yaml.in/yaml/v2 . `go.yaml.in/yaml/v2`/Unmarshal().([]byte(datascip-go gomod go.yaml.in/yaml/v2 . `go.yaml.in/yaml/v2_test`/data.), &b) |
| 33 | if err != nil { |
| 34 | logscip-go gomod github.com/golang/go/src go1.15 log/.Fatalfscip-go gomod github.com/golang/go/src go1.15 log/Fatalf().("cannot unmarshal data: %v", err) |
| 35 | } |
| 36 | fmtscip-go gomod github.com/golang/go/src go1.15 fmt/.Printlnscip-go gomod github.com/golang/go/src go1.15 fmt/Println().(b.Ascip-go gomod go.yaml.in/yaml/v2 . `go.yaml.in/yaml/v2_test`/StructA#A.) |
| 37 | fmtscip-go gomod github.com/golang/go/src go1.15 fmt/.Printlnscip-go gomod github.com/golang/go/src go1.15 fmt/Println().(b.Bscip-go gomod go.yaml.in/yaml/v2 . `go.yaml.in/yaml/v2_test`/StructB#B.) |
| 38 | // Output: |
| 39 | // a string from struct A |
| 40 | // a string from struct B |
| 41 | } |