Debian Codegraph @

example/multi_document_dumper/main.go

raw symbols SCIP
1 // Copyright 2025 The go-yaml Project Contributors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Example: Multi-Document Dumper demonstrates dumping multiple YAML documents.
5
6 package main
7
8 import (
9 "bytes"
10 "fmt"
11
12 "go.yaml.in/yaml/v4"
13 )
14
15 type Config struct {
16 Name string `yaml:"name"`
17 Version string `yaml:"version"`
18 Tags []string `yaml:"tags,omitempty"`
19 }
20
21 func main() {
22 fmt.Println("Example 5: Dumper - Multiple Documents")
23
24 var buf bytes.Buffer
25 dumper, err := yaml.NewDumper(&buf)
26 if err != nil {
27 panic(err)
28 }
29
30 configs := []Config{
31 {Name: "service1", Version: "1.0.0"},
32 {Name: "service2", Version: "2.0.0", Tags: []string{"dev"}},
33 {Name: "service3", Version: "3.0.0"},
34 }
35
36 for _, cfg := range configs {
37 if err := dumper.Dump(&cfg); err != nil {
38 panic(err)
39 }
40 }
41
42 if err := dumper.Close(); err != nil {
43 panic(err)
44 }
45
46 fmt.Printf("Output:\n%s", buf.String())
47 }