Debian Codegraph @

example/node_programmatic_build/main.go

raw symbols SCIP
1 // Copyright 2025 The go-yaml Project Contributors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Node Programmatic Build demonstrates building YAML Nodes programmatically.
5
6 package main
7
8 import (
9 "fmt"
10 "log"
11
12 "go.yaml.in/yaml/v4"
13 )
14
15 func main() {
16 fmt.Println("=== Building YAML Nodes Programmatically ===")
17
18 // Build a complex structure
19 environments := map[string]any{
20 "development": map[string]any{
21 "database": "dev.db",
22 "debug": true,
23 },
24 "production": map[string]any{
25 "database": "prod.db",
26 "debug": false,
27 },
28 }
29
30 // Convert to node with specific formatting
31 var node yaml.Node
32 err := node.Dump(environments,
33 yaml.WithIndent(2),
34 yaml.WithCompactSeqIndent(),
35 yaml.WithExplicitStart(),
36 )
37 if err != nil {
38 log.Fatal(err)
39 }
40
41 // Now we can manipulate the node or output it
42 fmt.Println("Generated YAML with custom formatting:")
43 data, err := yaml.Dump(&node)
44 if err != nil {
45 log.Fatal(err)
46 }
47 fmt.Printf("%s\n", data)
48
49 // We can also load it back with options
50 var result map[string]any
51 if err := node.Load(&result); err != nil {
52 log.Fatal(err)
53 }
54 fmt.Printf("Loaded back: %+v\n", result)
55 }