| 1 | # go-yaml Examples |
| 2 | |
| 3 | This directory contains examples demonstrating various features of the |
| 4 | go-yaml library. |
| 5 | |
| 6 | ## Running Examples |
| 7 | |
| 8 | Each example can be run directly: |
| 9 | |
| 10 | ```bash |
| 11 | go run example/basic_loader.go |
| 12 | ``` |
| 13 | |
| 14 | Or from within the example directory: |
| 15 | |
| 16 | ```bash |
| 17 | cd example |
| 18 | go run basic_loader.go |
| 19 | ``` |
| 20 | |
| 21 | ## Basic Examples |
| 22 | |
| 23 | ### Loading YAML |
| 24 | |
| 25 | **`basic_loader.go`** - Simple YAML loading with Loader |
| 26 | - Creates a Loader from a reader |
| 27 | - Loads YAML into Go structs |
| 28 | - Basic unmarshaling example |
| 29 | |
| 30 | **`load_into_node.go`** - Load YAML into Node representation |
| 31 | - Uses Node type for low-level YAML access |
| 32 | - Demonstrates working with the YAML AST |
| 33 | |
| 34 | **`multi_document_loader.go`** - Load multiple YAML documents |
| 35 | - Processes multi-document streams |
| 36 | - Loops through documents until EOF |
| 37 | |
| 38 | **`single_document_loader.go`** - Load only first document |
| 39 | - Uses WithSingleDocument option |
| 40 | - Subsequent Load() calls return EOF |
| 41 | |
| 42 | ### Dumping YAML |
| 43 | |
| 44 | **`basic_dumper.go`** - Simple YAML dumping with Dumper |
| 45 | - Creates a Dumper to a writer |
| 46 | - Dumps Go structs to YAML |
| 47 | |
| 48 | **`multi_document_dumper.go`** - Dump multiple YAML documents |
| 49 | - Creates a multi-document stream |
| 50 | - Multiple Dump() calls separated by `---` |
| 51 | |
| 52 | **`dumper_with_indent.go`** - Custom indentation |
| 53 | - Uses WithIndent option |
| 54 | - Demonstrates formatting control |
| 55 | |
| 56 | **`dumper_indent_comparison.go`** - Compare different indent levels |
| 57 | - Shows output with 2, 4, and 8-space indentation |
| 58 | |
| 59 | ## Options System Examples |
| 60 | |
| 61 | **`with_v4_option.go`** - Using v4 option presets |
| 62 | - Demonstrates yaml.WithV4Defaults() |
| 63 | - Shows v4 defaults (2-space indent) |
| 64 | - Compares with default (v3) output |
| 65 | |
| 66 | **`with_v4_override.go`** - Overriding option presets |
| 67 | - Combines version presets with individual options |
| 68 | - Demonstrates left-to-right option application |
| 69 | - Shows how later options override earlier ones |
| 70 | |
| 71 | **`multiple_options_loader.go`** - Combining multiple options |
| 72 | - Uses WithSingleDocument and WithKnownFields together |
| 73 | - Demonstrates strict field checking |
| 74 | - Shows error handling for unknown fields |
| 75 | |
| 76 | ## Node-Level API Examples |
| 77 | |
| 78 | **`node_load_strict_unmarshaler/main.go`** - Strict field checking in custom |
| 79 | unmarshalers |
| 80 | - Solves Issue #460 - preserving options in custom UnmarshalYAML |
| 81 | - Uses node.Load() with WithKnownFields() |
| 82 | - Demonstrates proper error handling for unknown fields |
| 83 | |
| 84 | **`node_dump_with_options/main.go`** - Encoding nodes with different options |
| 85 | - Shows node.Dump() with various formatting options |
| 86 | - Compares default, v3, and custom indent styles |
| 87 | - Demonstrates explicit start markers |
| 88 | |
| 89 | **`node_load_decode_comparison/main.go`** - Compare Decode() vs Load() |
| 90 | - Side-by-side comparison of old and new approaches |
| 91 | - Shows how node.Decode() loses options |
| 92 | - Demonstrates why node.Load() solves Issue #460 |
| 93 | |
| 94 | **`node_programmatic_build/main.go`** - Build YAML programmatically |
| 95 | - Build complex Node structures with options |
| 96 | - Shows round-trip (Dump then Load) with options |
| 97 | - Demonstrates node manipulation workflow |
| 98 | |
| 99 | ## Complete Demo |
| 100 | |
| 101 | **`loader_dumper_demo.go`** - Comprehensive feature demonstration |
| 102 | - Covers Loader, Dumper, and options |
| 103 | - Multiple examples in one file |
| 104 | - Good overview of library capabilities |
| 105 | |
| 106 | ## Common Patterns |
| 107 | |
| 108 | ### Basic Load and Dump |
| 109 | |
| 110 | ```go |
| 111 | import "go.yaml.in/yaml/v4" |
| 112 | |
| 113 | // Load |
| 114 | var config Config |
| 115 | yaml.Load(yamlData, &config) |
| 116 | |
| 117 | // Dump |
| 118 | data, _ := yaml.Dump(&config) |
| 119 | ``` |
| 120 | |
| 121 | ### Streaming with Options |
| 122 | |
| 123 | ```go |
| 124 | // Load with options |
| 125 | loader, _ := yaml.NewLoader(reader, |
| 126 | yaml.WithKnownFields(), |
| 127 | yaml.WithSingleDocument(), |
| 128 | ) |
| 129 | loader.Load(&config) |
| 130 | |
| 131 | // Dump with options |
| 132 | dumper, _ := yaml.NewDumper(writer, |
| 133 | yaml.WithIndent(2), |
| 134 | ) |
| 135 | dumper.Dump(&config) |
| 136 | dumper.Close() |
| 137 | ``` |
| 138 | |
| 139 | ### Using Version Presets |
| 140 | |
| 141 | ```go |
| 142 | dumper, _ := yaml.NewDumper(writer, |
| 143 | yaml.WithV4Defaults(), |
| 144 | ) |
| 145 | ``` |
| 146 | |
| 147 | ## Learn More |
| 148 | |
| 149 | - See the [main package documentation](https://pkg.go.dev/go.yaml.in/yaml/v4) |
| 150 | for API reference |
| 151 | - Run `make doc-serve` from the project root to view local documentation |
| 152 | - Check individual example source code for detailed comments |