Documentation
¶
Overview ¶
Package yaml implements YAML 1.1/1.2 encoding and decoding for Go programs.
Quick Start ¶
For simple encoding and decoding, use Unmarshal and Marshal:
type Config struct {
Name string `yaml:"name"`
Version string `yaml:"version"`
}
// Decode YAML to Go struct
var config Config
err := yaml.Unmarshal(yamlData, &config)
// Encode Go struct to YAML
data, err := yaml.Marshal(&config)
For encoding/decoding with options, use Load and Dump:
// Decode with strict field checking
err := yaml.Load(data, &config, yaml.WithKnownFields())
// Encode with custom indent
data, err := yaml.Dump(&config, yaml.WithIndent(2))
// Decode all documents from multi-document stream
var docs []Config
err := yaml.Load(multiDocYAML, &docs, yaml.WithAllDocuments())
// Encode multiple documents as multi-document stream
docs := []Config{config1, config2}
data, err := yaml.Dump(docs, yaml.WithAllDocuments())
Streaming with Loader and Dumper ¶
For multi-document streams or when you need custom options, use Loader and Dumper:
// Load multiple documents from a stream
loader, err := yaml.NewLoader(reader)
if err != nil {
log.Fatal(err)
}
for {
var doc any
if err := loader.Load(&doc); err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
// Process document...
}
// Dump multiple documents to a stream
dumper, err := yaml.NewDumper(writer, yaml.WithIndent(2))
if err != nil {
log.Fatal(err)
}
dumper.Dump(&doc1)
dumper.Dump(&doc2)
dumper.Close()
Options System ¶
Configure YAML processing behavior with functional options:
yaml.NewDumper(w,
yaml.WithIndent(2), // Indentation spacing
yaml.WithCompactSeqIndent(), // Compact sequences (defaults to true)
yaml.WithLineWidth(80), // Line wrapping width
yaml.WithUnicode(false), // Escape non-ASCII (override default true)
yaml.WithKnownFields(), // Strict field checking (defaults to true)
yaml.WithUniqueKeys(), // Prevent duplicate keys (defaults to true)
yaml.WithSingleDocument(), // Single document mode
)
Or use version-specific option presets for consistent formatting:
yaml.NewDumper(w, yaml.V3)
Options can be combined and later options override earlier ones:
// Start with v3 defaults, then override indent
yaml.NewDumper(w,
yaml.V3,
yaml.WithIndent(4),
)
Load options from YAML configuration files:
opts, err := yaml.OptsYAML(configYAML) dumper, err := yaml.NewDumper(w, opts)
YAML Compatibility ¶
This package supports most of YAML 1.2, but preserves some YAML 1.1 behavior for backward compatibility:
- YAML 1.1 booleans (yes/no, on/off) are supported when decoding into typed bool values, otherwise treated as strings
- Octals can use 0777 format (YAML 1.1) or 0o777 format (YAML 1.2)
- Base-60 floats are not supported (removed in YAML 1.2)
Version Defaults ¶
NewLoader and NewDumper use v4 defaults (2-space indentation, compact sequences). The older Marshal and Unmarshal functions use v3 defaults for backward compatibility. Use the options system to select different version defaults if needed.
Index ¶
- Constants
- Variables
- func Dump(in any, opts ...Option) (out []byte, err error)
- func Load(in []byte, out any, opts ...Option) error
- func Marshal(in any) (out []byte, err error)
- func Unmarshal(in []byte, out any) (err error)
- type Decoder
- type Dumper
- type Encoder
- type Encoding
- type IsZeroer
- type Kind
- type LineBreak
- type LoadError
- type LoadErrors
- type Loader
- type Marshaler
- type Node
- type Option
- type QuoteStyle
- type Style
- type TagDirective
- type TypeErrordeprecated
- type Unmarshaler
- type VersionDirective
Constants ¶
const ( EncodingAny = libyaml.ANY_ENCODING EncodingUTF8 = libyaml.UTF8_ENCODING EncodingUTF16LE = libyaml.UTF16LE_ENCODING EncodingUTF16BE = libyaml.UTF16BE_ENCODING )
Re-export encoding constants
const ( DocumentNode = libyaml.DocumentNode SequenceNode = libyaml.SequenceNode MappingNode = libyaml.MappingNode ScalarNode = libyaml.ScalarNode AliasNode = libyaml.AliasNode StreamNode = libyaml.StreamNode )
Re-export Kind constants
const ( TaggedStyle = libyaml.TaggedStyle DoubleQuotedStyle = libyaml.DoubleQuotedStyle SingleQuotedStyle = libyaml.SingleQuotedStyle LiteralStyle = libyaml.LiteralStyle FoldedStyle = libyaml.FoldedStyle FlowStyle = libyaml.FlowStyle )
Re-export Style constants
const ( LineBreakLN = libyaml.LN_BREAK // Unix-style \n (default) LineBreakCR = libyaml.CR_BREAK // Old Mac-style \r LineBreakCRLN = libyaml.CRLN_BREAK // Windows-style \r\n )
Line break constants for different platforms.
const ( QuoteSingle = libyaml.QuoteSingle // Prefer single quotes (v4 default) QuoteDouble = libyaml.QuoteDouble // Prefer double quotes QuoteLegacy = libyaml.QuoteLegacy // Legacy v2/v3 behavior )
Quote style constants for required quoting.
Variables ¶
var ( // WithIndent sets indentation spaces (2-9). // See internal/libyaml.WithIndent. WithIndent = libyaml.WithIndent // WithCompactSeqIndent configures '- ' as part of indentation. // See internal/libyaml.WithCompactSeqIndent. WithCompactSeqIndent = libyaml.WithCompactSeqIndent // WithKnownFields enables strict field checking during loading. // See internal/libyaml.WithKnownFields. WithKnownFields = libyaml.WithKnownFields // WithSingleDocument only processes first document in stream. // See internal/libyaml.WithSingleDocument. WithSingleDocument = libyaml.WithSingleDocument // WithStreamNodes enables stream boundary nodes when loading. // See internal/libyaml.WithStreamNodes. WithStreamNodes = libyaml.WithStreamNodes // WithAllDocuments enables multi-document mode for Load and Dump. // See internal/libyaml.WithAllDocuments. WithAllDocuments = libyaml.WithAllDocuments // WithLineWidth sets preferred line width for output. // See internal/libyaml.WithLineWidth. WithLineWidth = libyaml.WithLineWidth // WithUnicode controls non-ASCII characters in output. // See internal/libyaml.WithUnicode. WithUnicode = libyaml.WithUnicode // WithUniqueKeys enables duplicate key detection. // See internal/libyaml.WithUniqueKeys. WithUniqueKeys = libyaml.WithUniqueKeys // WithCanonical forces canonical YAML output format. // See internal/libyaml.WithCanonical. WithCanonical = libyaml.WithCanonical // WithLineBreak sets line ending style for output. // See internal/libyaml.WithLineBreak. WithLineBreak = libyaml.WithLineBreak // WithExplicitStart controls document start markers (---). // See internal/libyaml.WithExplicitStart. WithExplicitStart = libyaml.WithExplicitStart // WithExplicitEnd controls document end markers (...). // See internal/libyaml.WithExplicitEnd. WithExplicitEnd = libyaml.WithExplicitEnd // WithFlowSimpleCollections controls flow style for simple collections. // See internal/libyaml.WithFlowSimpleCollections. WithFlowSimpleCollections = libyaml.WithFlowSimpleCollections // WithQuotePreference sets preferred quote style when quoting is required. // See internal/libyaml.WithQuotePreference. WithQuotePreference = libyaml.WithQuotePreference )
var V2 = Options( WithIndent(2), WithCompactSeqIndent(false), WithLineWidth(80), WithUnicode(true), WithUniqueKeys(true), WithQuotePreference(QuoteLegacy), )
V2 defaults:
var V3 = Options( WithIndent(4), WithCompactSeqIndent(false), WithLineWidth(80), WithUnicode(true), WithUniqueKeys(true), WithQuotePreference(QuoteLegacy), )
V3 defaults:
var V4 = Options( WithIndent(2), WithCompactSeqIndent(true), WithLineWidth(80), WithUnicode(true), WithUniqueKeys(true), WithQuotePreference(QuoteSingle), )
V4 defaults:
Functions ¶
func Dump ¶
Dump encodes a value to YAML with the given options.
By default, Dump encodes a single value as a single YAML document.
Use WithAllDocuments() to encode multiple values as a multi-document stream:
docs := []Config{config1, config2, config3}
yaml.Dump(docs, yaml.WithAllDocuments())
When WithAllDocuments is used, in must be a slice. Each element is encoded as a separate YAML document with "---" separators.
See Marshal for details about the conversion of Go values to YAML.
func Load ¶
Load decodes YAML document(s) with the given options.
By default, Load requires exactly one document in the input. If zero documents are found, it returns an error. If multiple documents are found, it returns an error.
Use WithAllDocuments() to load all documents into a slice:
var configs []Config yaml.Load(multiDocYAML, &configs, yaml.WithAllDocuments())
When WithAllDocuments is used, out must be a pointer to a slice. Each document is decoded into the slice element type. Zero documents results in an empty slice (no error).
Maps and pointers (to a struct, string, int, etc) are accepted as out values. If an internal pointer within a struct is not initialized, the yaml package will initialize it if necessary. The out parameter must not be nil.
The type of the decoded values should be compatible with the respective values in out. If one or more values cannot be decoded due to type mismatches, decoding continues partially until the end of the YAML content, and a *yaml.LoadErrors is returned with details for all missed values.
Struct fields are only loaded if they are exported (have an upper case first letter), and are loaded using the field name lowercased as the default key. Custom keys may be defined via the "yaml" name in the field tag: the content preceding the first comma is used as the key, and the following comma-separated options control the loading and dumping behavior.
For example:
type T struct {
F int `yaml:"a,omitempty"`
B int
}
var t T
yaml.Load([]byte("a: 1\nb: 2"), &t)
See the documentation of Dump for the format of tags and a list of supported tag options.
func Marshal ¶
Marshal serializes the value provided into a YAML document. The structure of the generated document will reflect the structure of the value itself. Maps and pointers (to struct, string, int, etc) are accepted as the in value.
Struct fields are only marshaled if they are exported (have an upper case first letter), and are marshaled using the field name lowercased as the default key. Custom keys may be defined via the "yaml" name in the field tag: the content preceding the first comma is used as the key, and the following comma-separated options are used to tweak the marshaling process. Conflicting names result in a runtime error.
The field tag format accepted is:
`(...) yaml:"[<key>][,<flag1>[,<flag2>]]" (...)`
The following flags are currently supported:
omitempty Only include the field if it's not set to the zero
value for the type or to empty slices or maps.
Zero valued structs will be omitted if all their public
fields are zero, unless they implement an IsZero
method (see the IsZeroer interface type), in which
case the field will be excluded if IsZero returns true.
flow Marshal using a flow style (useful for structs,
sequences and maps).
inline Inline the field, which must be a struct or a map,
causing all of its fields or keys to be processed as if
they were part of the outer struct. For maps, keys must
not conflict with the yaml keys of other struct fields.
See doc/inline-tags.md for detailed examples and use cases.
In addition, if the key is "-", the field is ignored.
For example:
type T struct {
F int `yaml:"a,omitempty"`
B int
}
yaml.Marshal(&T{B: 2}) // Returns "b: 2\n"
yaml.Marshal(&T{F: 1}} // Returns "a: 1\nb: 0\n"
func Unmarshal ¶
Unmarshal decodes the first document found within the in byte slice and assigns decoded values into the out value.
Maps and pointers (to a struct, string, int, etc) are accepted as out values. If an internal pointer within a struct is not initialized, the yaml package will initialize it if necessary for unmarshalling the provided data. The out parameter must not be nil.
The type of the decoded values should be compatible with the respective values in out. If one or more values cannot be decoded due to a type mismatches, decoding continues partially until the end of the YAML content, and a *yaml.LoadErrors is returned with details for all missed values.
Struct fields are only unmarshalled if they are exported (have an upper case first letter), and are unmarshalled using the field name lowercased as the default key. Custom keys may be defined via the "yaml" name in the field tag: the content preceding the first comma is used as the key, and the following comma-separated options are used to tweak the marshaling process (see Marshal). Conflicting names result in a runtime error.
For example:
type T struct {
F int `yaml:"a,omitempty"`
B int
}
var t T
yaml.Construct([]byte("a: 1\nb: 2"), &t)
See the documentation of Marshal for the format of tags and a list of supported tag options.
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
A Decoder reads and decodes YAML values from an input stream.
func NewDecoder ¶
NewDecoder returns a new decoder that reads from r.
The decoder introduces its own buffering and may read data from r beyond the YAML values requested.
func (*Decoder) Decode ¶
Decode reads the next YAML-encoded value from its input and stores it in the value pointed to by v.
See the documentation for Unmarshal for details about the conversion of YAML into a Go value.
func (*Decoder) KnownFields ¶
KnownFields ensures that the keys in decoded mappings to exist as fields in the struct being decoded into.
type Dumper ¶
type Dumper struct {
// contains filtered or unexported fields
}
A Dumper writes YAML values to an output stream with configurable options.
func NewDumper ¶
NewDumper returns a new Dumper that writes to w with the given options.
The Dumper should be closed after use to flush all data to w.
func (*Dumper) Close ¶
Close closes the Dumper by writing any remaining data. It does not write a stream terminating string "...".
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
An Encoder writes YAML values to an output stream.
func NewEncoder ¶
NewEncoder returns a new encoder that writes to w. The Encoder should be closed after use to flush all data to w.
func (*Encoder) Close ¶
Close closes the encoder by writing any remaining data. It does not write a stream terminating string "...".
func (*Encoder) CompactSeqIndent ¶
func (e *Encoder) CompactSeqIndent()
CompactSeqIndent makes it so that '- ' is considered part of the indentation.
func (*Encoder) DefaultSeqIndent ¶
func (e *Encoder) DefaultSeqIndent()
DefaultSeqIndent makes it so that '- ' is not considered part of the indentation.
func (*Encoder) Encode ¶
Encode writes the YAML encoding of v to the stream. If multiple items are encoded to the stream, the second and subsequent document will be preceded with a "---" document separator, but the first will not.
See the documentation for Marshal for details about the conversion of Go values to YAML.
type IsZeroer ¶
IsZeroer is implemented by types that can report if they're zero. See internal/libyaml.IsZeroer.
type LoadError ¶
type LoadError = libyaml.ConstructError
LoadError represents an error encountered while decoding a YAML document.
It contains details about the location in the document where the error occurred, as well as a descriptive message.
type LoadErrors ¶
type LoadErrors = libyaml.LoadErrors
LoadErrors is returned when one or more fields cannot be properly decoded.
It contains multiple *LoadError instances with details about each error.
type Loader ¶
type Loader struct {
// contains filtered or unexported fields
}
A Loader reads and decodes YAML values from an input stream with configurable options.
func NewLoader ¶
NewLoader returns a new Loader that reads from r with the given options.
The Loader introduces its own buffering and may read data from r beyond the YAML values requested.
func (*Loader) Load ¶
Load reads the next YAML-encoded document from its input and stores it in the value pointed to by v.
Returns io.EOF when there are no more documents to read. If WithSingleDocument option was set and a document was already read, subsequent calls return io.EOF.
Maps and pointers (to a struct, string, int, etc) are accepted as v values. If an internal pointer within a struct is not initialized, the yaml package will initialize it if necessary. The v parameter must not be nil.
Struct fields are only loaded if they are exported (have an upper case first letter), and are loaded using the field name lowercased as the default key. Custom keys may be defined via the "yaml" name in the field tag: the content preceding the first comma is used as the key, and the following comma-separated options control the loading and dumping behavior.
See the documentation of the package-level Load function for more details about YAML to Go conversion and tag options.
type Marshaler ¶
Marshaler is implemented by types with custom YAML marshaling. See internal/libyaml.Marshaler.
type Option ¶
Option allows configuring YAML loading and dumping operations. Re-exported from internal/libyaml.
func Options ¶
Options combines multiple options into a single Option. This is useful for creating option presets or combining version defaults with custom options.
Example:
opts := yaml.Options(yaml.V4, yaml.WithIndent(3)) yaml.Dump(&data, opts)
func OptsYAML ¶
OptsYAML parses a YAML string containing option settings and returns an Option that can be combined with other options using Options().
The YAML string can specify any of these fields: - indent (int) - compact-seq-indent (bool) - line-width (int) - unicode (bool) - canonical (bool) - line-break (string: ln, cr, crln) - explicit-start (bool) - explicit-end (bool) - flow-simple-coll (bool) - known-fields (bool) - single-document (bool) - unique-keys (bool)
Only fields specified in the YAML will override other options when combined. Unspecified fields won't affect other options.
Example:
opts, err := yaml.OptsYAML(` indent: 3 known-fields: true `) yaml.Dump(&data, yaml.Options(V4, opts))
type QuoteStyle ¶
type QuoteStyle = libyaml.QuoteStyle
QuoteStyle represents the quote style to use when quoting is required.
type TypeError
deprecated
TypeError is an obsolete error type retained for compatibility.
Deprecated: Use LoadErrors instead.
type Unmarshaler ¶
Unmarshaler is the interface implemented by types that can unmarshal a YAML description of themselves.
type VersionDirective ¶
type VersionDirective = libyaml.StreamVersionDirective
Re-export stream-related types
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
go-yaml
command
|
|
|
example
|
|
|
basic_dumper
command
|
|
|
basic_loader
command
|
|
|
dumper_indent_comparison
command
|
|
|
dumper_with_indent
command
|
|
|
load_into_node
command
|
|
|
loader_dumper_demo
command
|
|
|
multi_document_dumper
command
|
|
|
multi_document_loader
command
|
|
|
multiple_options_loader
command
|
|
|
node_dump_with_options
command
|
|
|
node_load_decode_comparison
command
|
|
|
node_load_strict_unmarshaler
command
|
|
|
node_programmatic_build
command
|
|
|
single_document_loader
command
|
|
|
version_options
command
|
|
|
with_v4_option
command
|
|
|
with_v4_override
command
|
|
|
internal
|
|
|
libyaml
Package libyaml contains internal helpers for working with YAML
|
Package libyaml contains internal helpers for working with YAML |
|
testutil/assert
Package assert provides assertion functions for tests.
|
Package assert provides assertion functions for tests. |
|
testutil/datatest
Package datatest provides utilities for data-driven testing with YAML test files.
|
Package datatest provides utilities for data-driven testing with YAML test files. |