| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package main |
| 7 |
|
| 8 |
import ( |
| 9 |
"fmt" |
| 10 |
|
| 11 |
"go.yaml.in/yaml/v4" |
| 12 |
) |
| 13 |
|
| 14 |
type Config struct { |
| 15 |
Name string `yaml:"name"` |
| 16 |
Items []string `yaml:"items"` |
| 17 |
} |
| 18 |
|
| 19 |
func main() { |
| 20 |
cfg := Config{ |
| 21 |
Name: "test", |
| 22 |
Items: []string{"apple", "banana", "cherry"}, |
| 23 |
} |
| 24 |
|
| 25 |
fmt.Println("Example: Comparing v2, v3, and v4 option presets") |
| 26 |
|
| 27 |
|
| 28 |
fmt.Println("=== yaml.WithV2Defaults() - 2-space indent, non-compact sequences ===") |
| 29 |
out, _ := yaml.Dump(&cfg, yaml.WithV2Defaults()) |
| 30 |
fmt.Print(string(out)) |
| 31 |
|
| 32 |
|
| 33 |
fmt.Println("=== yaml.WithV3Defaults() - 4-space indent, non-compact sequences ===") |
| 34 |
out, _ = yaml.Dump(&cfg, yaml.WithV3Defaults()) |
| 35 |
fmt.Print(string(out)) |
| 36 |
|
| 37 |
|
| 38 |
fmt.Println("=== yaml.WithV4Defaults() - 2-space indent, compact sequences ===") |
| 39 |
out, _ = yaml.Dump(&cfg, yaml.WithV4Defaults()) |
| 40 |
fmt.Print(string(out)) |
| 41 |
|
| 42 |
|
| 43 |
fmt.Println("=== yaml.WithV4Defaults() with WithIndent(3) override ===") |
| 44 |
out, _ = yaml.Dump(&cfg, yaml.WithV4Defaults(), yaml.WithIndent(3)) |
| 45 |
fmt.Print(string(out)) |
| 46 |
|
| 47 |
fmt.Println("\nNotice how:") |
| 48 |
fmt.Println("- v2 and v4 both use 2-space indentation") |
| 49 |
fmt.Println("- v3 uses 4-space indentation (classic go-yaml v3 style)") |
| 50 |
fmt.Println("- v4 uses compact sequences (items: flows from dash)") |
| 51 |
fmt.Println("- Options can be combined and later ones win") |
| 52 |
} |