| 1 | // Copyright 2025 The go-yaml Project Contributors |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| 4 | // Package yaml implements YAML 1.1/1.2 encoding and decoding for Go programs. |
| 5 | // |
| 6 | // # Quick Start |
| 7 | // |
| 8 | // For simple encoding and decoding, use [Unmarshal] and [Marshal]: |
| 9 | // |
| 10 | // type Config struct { |
| 11 | // Name string `yaml:"name"` |
| 12 | // Version string `yaml:"version"` |
| 13 | // } |
| 14 | // |
| 15 | // // Decode YAML to Go struct |
| 16 | // var config Config |
| 17 | // err := yaml.Unmarshal(yamlData, &config) |
| 18 | // |
| 19 | // // Encode Go struct to YAML |
| 20 | // data, err := yaml.Marshal(&config) |
| 21 | // |
| 22 | // For encoding/decoding with options, use [Load] and [Dump]: |
| 23 | // |
| 24 | // // Decode with strict field checking |
| 25 | // err := yaml.Load(data, &config, yaml.WithKnownFields()) |
| 26 | // |
| 27 | // // Encode with custom indent |
| 28 | // data, err := yaml.Dump(&config, yaml.WithIndent(2)) |
| 29 | // |
| 30 | // // Decode all documents from multi-document stream |
| 31 | // var docs []Config |
| 32 | // err := yaml.Load(multiDocYAML, &docs, yaml.WithAllDocuments()) |
| 33 | // |
| 34 | // // Encode multiple documents as multi-document stream |
| 35 | // docs := []Config{config1, config2} |
| 36 | // data, err := yaml.Dump(docs, yaml.WithAllDocuments()) |
| 37 | // |
| 38 | // # Streaming with Loader and Dumper |
| 39 | // |
| 40 | // For multi-document streams or when you need custom options, use [Loader] and [Dumper]: |
| 41 | // |
| 42 | // // Load multiple documents from a stream |
| 43 | // loader, err := yaml.NewLoader(reader) |
| 44 | // if err != nil { |
| 45 | // log.Fatal(err) |
| 46 | // } |
| 47 | // for { |
| 48 | // var doc any |
| 49 | // if err := loader.Load(&doc); err == io.EOF { |
| 50 | // break |
| 51 | // } else if err != nil { |
| 52 | // log.Fatal(err) |
| 53 | // } |
| 54 | // // Process document... |
| 55 | // } |
| 56 | // |
| 57 | // // Dump multiple documents to a stream |
| 58 | // dumper, err := yaml.NewDumper(writer, yaml.WithIndent(2)) |
| 59 | // if err != nil { |
| 60 | // log.Fatal(err) |
| 61 | // } |
| 62 | // dumper.Dump(&doc1) |
| 63 | // dumper.Dump(&doc2) |
| 64 | // dumper.Close() |
| 65 | // |
| 66 | // # Options System |
| 67 | // |
| 68 | // Configure YAML processing behavior with functional options: |
| 69 | // |
| 70 | // yaml.NewDumper(w, |
| 71 | // yaml.WithIndent(2), // Indentation spacing |
| 72 | // yaml.WithCompactSeqIndent(), // Compact sequences (defaults to true) |
| 73 | // yaml.WithLineWidth(80), // Line wrapping width |
| 74 | // yaml.WithUnicode(false), // Escape non-ASCII (override default true) |
| 75 | // yaml.WithKnownFields(), // Strict field checking (defaults to true) |
| 76 | // yaml.WithUniqueKeys(), // Prevent duplicate keys (defaults to true) |
| 77 | // yaml.WithSingleDocument(), // Single document mode |
| 78 | // ) |
| 79 | // |
| 80 | // Or use version-specific option presets for consistent formatting: |
| 81 | // |
| 82 | // yaml.NewDumper(w, yaml.WithV3Defaults()) |
| 83 | // |
| 84 | // Options can be combined and later options override earlier ones: |
| 85 | // |
| 86 | // // Start with v3 defaults, then override indent |
| 87 | // yaml.NewDumper(w, |
| 88 | // yaml.WithV3Defaults(), |
| 89 | // yaml.WithIndent(2), |
| 90 | // ) |
| 91 | // |
| 92 | // Load options from YAML configuration files: |
| 93 | // |
| 94 | // opts, err := yaml.OptsYAML(configYAML) |
| 95 | // dumper, err := yaml.NewDumper(w, opts) |
| 96 | // |
| 97 | // # YAML Compatibility |
| 98 | // |
| 99 | // This package supports most of YAML 1.2, but preserves some YAML 1.1 |
| 100 | // behavior for backward compatibility: |
| 101 | // |
| 102 | // - YAML 1.1 booleans (yes/no, on/off) are supported when decoding into |
| 103 | // typed bool values, otherwise treated as strings |
| 104 | // - Octals can use 0777 format (YAML 1.1) or 0o777 format (YAML 1.2) |
| 105 | // - Base-60 floats are not supported (removed in YAML 1.2) |
| 106 | // |
| 107 | // # Version Defaults |
| 108 | // |
| 109 | // [NewLoader] and [NewDumper] use v4 defaults (2-space indentation, compact |
| 110 | // sequences). The older [Marshal] and [Unmarshal] functions use v3 defaults |
| 111 | // for backward compatibility. Use the options system to select different |
| 112 | // version defaults if needed. |
| 113 | package yaml |