File: example_embedded_test.go

package info (click to toggle)
golang-github-jesseduffield-yaml 2.2.2+git20190702.b900b7e-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 436 kB
  • sloc: makefile: 2
file content (41 lines) | stat: -rw-r--r-- 725 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package yaml_test

import (
	"fmt"
	"log"

	"github.com/jesseduffield/yaml"
)

// 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
	StructA `yaml:",inline"`
	B       string `yaml:"b"`
}

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 {
		log.Fatalf("cannot unmarshal data: %v", err)
	}
	fmt.Println(b.A)
	fmt.Println(b.B)
	// Output:
	// a string from struct A
	// a string from struct B
}