| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package main |
| 7 |
|
| 8 |
import ( |
| 9 |
"fmt" |
| 10 |
"strings" |
| 11 |
|
| 12 |
"go.yaml.in/yaml/v4" |
| 13 |
) |
| 14 |
|
| 15 |
func main() { |
| 16 |
fmt.Println("Example: Load into yaml.Node") |
| 17 |
|
| 18 |
yamlData := `# Application configuration |
| 19 |
name: myapp # The app name |
| 20 |
version: 1.0.0 |
| 21 |
|
| 22 |
# Server settings |
| 23 |
server: |
| 24 |
host: localhost |
| 25 |
port: 8080 |
| 26 |
# Enable debug mode |
| 27 |
debug: true |
| 28 |
|
| 29 |
# List of enabled features |
| 30 |
features: |
| 31 |
- auth |
| 32 |
- logging |
| 33 |
- metrics |
| 34 |
` |
| 35 |
|
| 36 |
loader, err := yaml.NewLoader(strings.NewReader(yamlData)) |
| 37 |
if err != nil { |
| 38 |
panic(err) |
| 39 |
} |
| 40 |
|
| 41 |
var node yaml.Node |
| 42 |
if err := loader.Load(&node); err != nil { |
| 43 |
panic(err) |
| 44 |
} |
| 45 |
|
| 46 |
fmt.Println("Successfully loaded YAML into Node") |
| 47 |
fmt.Printf("Root node kind: %v\n", node.Kind) |
| 48 |
fmt.Printf("Root has %d child nodes (the document)\n\n", len(node.Content)) |
| 49 |
|
| 50 |
|
| 51 |
doc := node.Content[0] |
| 52 |
fmt.Printf("Document node kind: %v\n", doc.Kind) |
| 53 |
fmt.Printf("Document has %d children (key-value pairs)\n\n", len(doc.Content)) |
| 54 |
|
| 55 |
|
| 56 |
fmt.Println("Top-level keys with their values:") |
| 57 |
for i := 0; i < len(doc.Content); i += 2 { |
| 58 |
key := doc.Content[i] |
| 59 |
value := doc.Content[i+1] |
| 60 |
|
| 61 |
fmt.Printf("\nKey: %q\n", key.Value) |
| 62 |
if key.HeadComment != "" { |
| 63 |
fmt.Printf(" Head comment: %q\n", key.HeadComment) |
| 64 |
} |
| 65 |
if key.LineComment != "" { |
| 66 |
fmt.Printf(" Line comment: %q\n", key.LineComment) |
| 67 |
} |
| 68 |
|
| 69 |
switch value.Kind { |
| 70 |
case yaml.ScalarNode: |
| 71 |
fmt.Printf(" Value (scalar): %q\n", value.Value) |
| 72 |
if value.LineComment != "" { |
| 73 |
fmt.Printf(" Value line comment: %q\n", value.LineComment) |
| 74 |
} |
| 75 |
case yaml.MappingNode: |
| 76 |
fmt.Printf(" Value (mapping): %d key-value pairs\n", len(value.Content)/2) |
| 77 |
|
| 78 |
for j := 0; j < len(value.Content); j += 2 { |
| 79 |
nestedKey := value.Content[j] |
| 80 |
nestedValue := value.Content[j+1] |
| 81 |
fmt.Printf(" %s: %s", nestedKey.Value, nestedValue.Value) |
| 82 |
if nestedValue.LineComment != "" { |
| 83 |
fmt.Printf(" %s", nestedValue.LineComment) |
| 84 |
} |
| 85 |
fmt.Println() |
| 86 |
} |
| 87 |
case yaml.SequenceNode: |
| 88 |
fmt.Printf(" Value (sequence): %d items\n", len(value.Content)) |
| 89 |
for j, item := range value.Content { |
| 90 |
fmt.Printf(" [%d]: %s\n", j, item.Value) |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
fmt.Println("\n--- Modifying Node ---") |
| 97 |
|
| 98 |
newKey := &yaml.Node{ |
| 99 |
Kind: yaml.ScalarNode, |
| 100 |
Value: "environment", |
| 101 |
} |
| 102 |
newValue := &yaml.Node{ |
| 103 |
Kind: yaml.ScalarNode, |
| 104 |
Value: "production", |
| 105 |
} |
| 106 |
doc.Content = append(doc.Content, newKey, newValue) |
| 107 |
|
| 108 |
|
| 109 |
out, err := yaml.Dump(&node) |
| 110 |
if err != nil { |
| 111 |
panic(err) |
| 112 |
} |
| 113 |
|
| 114 |
fmt.Println("\nModified YAML (added 'environment' field):") |
| 115 |
fmt.Print(string(out)) |
| 116 |
} |