YAML support for the Go language
npx @tessl/cli install tessl/golang-gopkg-in--yaml-v3@3.0.0YAML support for the Go language, providing comprehensive encoding and decoding capabilities for YAML documents with support for most of YAML 1.2 specification while maintaining backwards compatibility with YAML 1.1.
go get gopkg.in/yaml.v3gopkg.in/yaml.v3import "gopkg.in/yaml.v3"For type-specific imports:
import (
"gopkg.in/yaml.v3"
)
// Use qualified names:
// yaml.Marshal()
// yaml.Unmarshal()
// yaml.Node{}Simple marshaling and unmarshaling:
package main
import (
"fmt"
"log"
"gopkg.in/yaml.v3"
)
type Config struct {
Name string
Version int
Options struct {
Debug bool
Timeout int `yaml:"timeout"`
}
}
func main() {
// Unmarshal YAML data
data := []byte(`
name: MyApp
version: 2
options:
debug: true
timeout: 30
`)
var config Config
err := yaml.Unmarshal(data, &config)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Config: %+v\n", config)
// Marshal to YAML
output, err := yaml.Marshal(&config)
if err != nil {
log.Fatal(err)
}
fmt.Printf("YAML output:\n%s", output)
}Core functionality for encoding and decoding YAML documents with simple function calls.
func Marshal(in interface{}) (out []byte, err error)func Unmarshal(in []byte, out interface{}) (err error)Stream-based encoding and decoding for processing multiple YAML documents or working with io.Reader/io.Writer interfaces.
func NewDecoder(r io.Reader) *Decoder
type Decoder struct {
// Has unexported fields
}
func (dec *Decoder) Decode(v interface{}) (err error)
func (dec *Decoder) KnownFields(enable bool)func NewEncoder(w io.Writer) *Encoder
type Encoder struct {
// Has unexported fields
}
func (e *Encoder) Encode(v interface{}) (err error)
func (e *Encoder) SetIndent(spaces int)
func (e *Encoder) Close() (err error)Low-level AST manipulation for fine-grained control over YAML document structure, comments, and formatting.
type Node struct {
Kind Kind
Style Style
Tag string
Value string
Anchor string
Alias *Node
Content []*Node
HeadComment string
LineComment string
FootComment string
Line int
Column int
}
func (n *Node) Decode(v interface{}) (err error)
func (n *Node) Encode(v interface{}) (err error)
func (n *Node) IsZero() bool
func (n *Node) LongTag() string
func (n *Node) ShortTag() string
func (n *Node) SetString(s string)Interfaces for implementing custom marshaling and unmarshaling behavior for user-defined types.
type Marshaler interface {
MarshalYAML() (interface{}, error)
}
type Unmarshaler interface {
UnmarshalYAML(value *Node) error
}
type IsZeroer interface {
IsZero() bool
}Returned when one or more fields cannot be properly decoded. Unmarshaling continues partially and returns all errors.
type TypeError struct {
Errors []string
}
func (e *TypeError) Error() stringDefines the type of YAML node.
type Kind uint32
const (
DocumentNode Kind = 1 << iota
SequenceNode
MappingNode
ScalarNode
AliasNode
)Controls the rendering style of YAML nodes.
type Style uint32
const (
TaggedStyle Style = 1 << iota
DoubleQuotedStyle
SingleQuotedStyle
LiteralStyle
FoldedStyle
FlowStyle
)This package supports most of YAML 1.2 while preserving some YAML 1.1 behavior:
0777 (YAML 1.1 format) rather than 0o777 (YAML 1.2). The 0o777 format is also supported for reading.Control marshaling and unmarshaling behavior with struct tags:
type Example struct {
Field1 string `yaml:"custom_name"` // Custom YAML key
Field2 int `yaml:"field2,omitempty"` // Omit if zero value
Field3 []int `yaml:"field3,flow"` // Use flow style
Field4 Data `yaml:",inline"` // Inline struct fields
Field5 string `yaml:"-"` // Ignore field
}omitempty - Omit field if zero value (respects IsZeroer interface)flow - Use flow style (inline) for sequences and mapsinline - Inline struct or map fields into parent- - Ignore field completely