Documentation ¶
Overview ¶
Package yaml implements YAML support for the Go language.
Source code and other details for the project are available at GitHub:
https://github.com/go-yaml/yaml
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 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 marshalling 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. Does not apply to zero valued structs. flow Marshal using a flow style (useful for structs, sequences and maps. inline Inline the struct it's applied to, so its fields are processed as if they were part of the outer struct.
In addition, if the key is "-", the field is ignored.
For example:
type T struct { F int "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 and the type of out will be considered, and Unmarshal will do the best possible job to unmarshal values appropriately. It is NOT considered an error, though, to skip values because they are not available in the decoded YAML, or if they are not compatible with the out value. To ensure something was properly unmarshaled use a map or compare against the previous value for the field (usually the zero value).
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 marshalling 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.Unmarshal([]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 Getter ¶
type Getter interface {
GetYAML() (tag string, value interface{})
}
The Getter interface is implemented by types to do their own custom marshalling into a YAML tag and value.
type Setter ¶
The Setter interface may be implemented by types to do their own custom unmarshalling of YAML values, rather than being implicitly assigned by the yaml package machinery. If setting the value works, the method should return true. If it returns false, the value is considered unsupported and is omitted from maps and slices.