Debian Codegraph @

example/basic_dumper/main.go

raw symbols SCIP
1 // Copyright 2025 The go-yaml Project Contributors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Example: Basic Dumper demonstrates simple YAML dumping from structs.
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 4: Basic Dumper - Single Document")
23
24 var buf bytes.Buffer
25 dumper, err := yaml.NewDumper(&buf)
26 if err != nil {
27 panic(err)
28 }
29
30 cfg := Config{
31 Name: "service1",
32 Version: "1.0.0",
33 Tags: []string{"prod"},
34 }
35
36 if err := dumper.Dump(&cfg); err != nil {
37 panic(err)
38 }
39
40 if err := dumper.Close(); err != nil {
41 panic(err)
42 }
43
44 fmt.Printf("Output:\n%s", buf.String())
45 }