Debian Codegraph @

example/dumper_with_indent/main.go

raw symbols SCIP
1 // Copyright 2025 The go-yaml Project Contributors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Example: Dumper With Indent demonstrates custom indentation settings.
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 6: Dumper with WithIndent Option")
23
24 var buf bytes.Buffer
25 dumper, err := yaml.NewDumper(&buf, yaml.WithIndent(4))
26 if err != nil {
27 panic(err)
28 }
29
30 cfg := Config{
31 Name: "service",
32 Version: "1.0.0",
33 Tags: []string{"a", "b", "c"},
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 (4-space indent):\n%s", buf.String())
45 }