1// 2// Copyright (c) 2011-2019 Canonical Ltd 3// 4// Licensed under the Apache License, Version 2.0 (the "License"); 5// you may not use this file except in compliance with the License. 6// You may obtain a copy of the License at 7// 8// http://www.apache.org/licenses/LICENSE-2.0 9// 10// Unless required by applicable law or agreed to in writing, software 11// distributed under the License is distributed on an "AS IS" BASIS, 12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13// See the License for the specific language governing permissions and 14// limitations under the License. 15 16package yaml_test 17 18import ( 19 "fmt" 20 "log" 21 22 "gopkg.in/yaml.v3" 23) 24 25// An example showing how to unmarshal embedded 26// structs from YAML. 27 28type StructA struct { 29 A string `yaml:"a"` 30} 31 32type StructB struct { 33 // Embedded structs are not treated as embedded in YAML by default. To do that, 34 // add the ",inline" annotation below 35 StructA `yaml:",inline"` 36 B string `yaml:"b"` 37} 38 39var data = ` 40a: a string from struct A 41b: a string from struct B 42` 43 44func ExampleUnmarshal_embedded() { 45 var b StructB 46 47 err := yaml.Unmarshal([]byte(data), &b) 48 if err != nil { 49 log.Fatalf("cannot unmarshal data: %v", err) 50 } 51 fmt.Println(b.A) 52 fmt.Println(b.B) 53 // Output: 54 // a string from struct A 55 // a string from struct B 56} 57