| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package main |
| 7 |
|
| 8 |
import ( |
| 9 |
"bytes" |
| 10 |
"fmt" |
| 11 |
"os" |
| 12 |
"strconv" |
| 13 |
|
| 14 |
"go.yaml.in/yaml/v4" |
| 15 |
) |
| 16 |
|
| 17 |
type Config struct { |
| 18 |
Name string `yaml:"name"` |
| 19 |
Version string `yaml:"version"` |
| 20 |
Server ServerConfig `yaml:"server"` |
| 21 |
Tags []string `yaml:"tags,omitempty"` |
| 22 |
Metadata map[string]string `yaml:"metadata,omitempty"` |
| 23 |
} |
| 24 |
|
| 25 |
type ServerConfig struct { |
| 26 |
Host string `yaml:"host"` |
| 27 |
Port int `yaml:"port"` |
| 28 |
Debug bool `yaml:"debug"` |
| 29 |
} |
| 30 |
|
| 31 |
func main() { |
| 32 |
cfg := Config{ |
| 33 |
Name: "myapp", |
| 34 |
Version: "1.0.0", |
| 35 |
Server: ServerConfig{ |
| 36 |
Host: "localhost", |
| 37 |
Port: 8080, |
| 38 |
Debug: true, |
| 39 |
}, |
| 40 |
Tags: []string{"web", "api", "production"}, |
| 41 |
Metadata: map[string]string{ |
| 42 |
"owner": "platform-team", |
| 43 |
"env": "prod", |
| 44 |
}, |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
if len(os.Args) > 1 { |
| 49 |
indent, err := strconv.Atoi(os.Args[1]) |
| 50 |
if err != nil { |
| 51 |
fmt.Fprintf(os.Stderr, "Error: invalid indent value %q (must be a number)\n", os.Args[1]) |
| 52 |
os.Exit(1) |
| 53 |
} |
| 54 |
|
| 55 |
if indent < 2 || indent > 9 { |
| 56 |
fmt.Fprintf(os.Stderr, "Error: indent value must be between 2 and 9 (got %d)\n", indent) |
| 57 |
os.Exit(1) |
| 58 |
} |
| 59 |
|
| 60 |
fmt.Printf("Example: Dumper with %d-space indent\n\n", indent) |
| 61 |
|
| 62 |
var buf bytes.Buffer |
| 63 |
dumper, err := yaml.NewDumper(&buf, yaml.WithIndent(indent)) |
| 64 |
if err != nil { |
| 65 |
panic(err) |
| 66 |
} |
| 67 |
|
| 68 |
if err := dumper.Dump(&cfg); err != nil { |
| 69 |
panic(err) |
| 70 |
} |
| 71 |
|
| 72 |
if err := dumper.Close(); err != nil { |
| 73 |
panic(err) |
| 74 |
} |
| 75 |
|
| 76 |
fmt.Print(buf.String()) |
| 77 |
return |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
fmt.Println("Example: Dumper with Different Indent Levels") |
| 82 |
fmt.Println("(Run with argument to test specific indent: go run dumper_indent_comparison.go 3)") |
| 83 |
|
| 84 |
indentLevels := []int{2, 4, 8} |
| 85 |
|
| 86 |
for _, indent := range indentLevels { |
| 87 |
fmt.Printf("--- Indent: %d spaces ---\n", indent) |
| 88 |
|
| 89 |
var buf bytes.Buffer |
| 90 |
dumper, err := yaml.NewDumper(&buf, yaml.WithIndent(indent)) |
| 91 |
if err != nil { |
| 92 |
panic(err) |
| 93 |
} |
| 94 |
|
| 95 |
if err := dumper.Dump(&cfg); err != nil { |
| 96 |
panic(err) |
| 97 |
} |
| 98 |
|
| 99 |
if err := dumper.Close(); err != nil { |
| 100 |
panic(err) |
| 101 |
} |
| 102 |
|
| 103 |
fmt.Print(buf.String()) |
| 104 |
fmt.Println() |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
fmt.Println("--- Default indent (no option) ---") |
| 109 |
var buf bytes.Buffer |
| 110 |
dumper, err := yaml.NewDumper(&buf) |
| 111 |
if err != nil { |
| 112 |
panic(err) |
| 113 |
} |
| 114 |
|
| 115 |
if err := dumper.Dump(&cfg); err != nil { |
| 116 |
panic(err) |
| 117 |
} |
| 118 |
|
| 119 |
if err := dumper.Close(); err != nil { |
| 120 |
panic(err) |
| 121 |
} |
| 122 |
|
| 123 |
fmt.Print(buf.String()) |
| 124 |
fmt.Println("\nTip: Run with a specific indent value as an argument:") |
| 125 |
fmt.Println(" go run example/dumper_indent_comparison.go 3") |
| 126 |
} |