YAML for Go
YAML 1.2 with zero dependencies — the one Go's stdlib never shipped.
A complete YAML 1.2 parser and emitter built entirely on the Go standard library — no third-party modules, no cgo, no require directives. This is the port with the clearest reason to exist: Go has no stdlib YAML, so every project reaches for a third-party module. <code>Marshal</code> and <code>Unmarshal</code> cover the one-shot cases, <code>NewDecoder</code> walks a multi-document stream and returns <code>io.EOF</code> after the last <code>---</code>, and <code>Node</code> is a lossless document model that keeps anchors, styles, line/column and head/line/foot comments so you can rewrite a file without flattening it. Tag resolution follows the YAML <b>1.2 core schema</b>, so <code>no</code> is the string "no" rather than false — the Norway problem does not apply. Verified against the language-agnostic yaml-test-suite. The import path is github.com/malcolmston/yaml.
Install
$ go get github.com/malcolmston/yamlQuick start
import "github.com/malcolmston/yaml"
var cfg struct {
Name string `yaml:"name"`
Ports []int `yaml:"ports"`
Env map[string]string `yaml:"env"`
}
if err := yaml.Unmarshal(src, &cfg); err != nil {
log.Fatal(err) <span class="tok-c">// *TypeError lists every bad field
}
fmt.Println(cfg.Ports[1])
out, _ := yaml.Marshal(cfg)Features
Marshal/Unmarshalonto structs, maps, slices andany, with struct tagsyaml:"name,omitempty",yaml:"-",yaml:",inline",yaml:",flow"- Streaming multi-document I/O —
NewDecoder(r).Decode(&v)returnsio.EOFpast the last document;NewEncoder(w)withSetIndentandClose - Full block and flow syntax — block and flow collections, explicit
?/:keys, block scalars|and>with chomping and explicit indent indicators, quoted scalars with the complete escape table - Anchors, aliases and merge keys (
&a,*a,<<) — with an alias-expansion budget, so a billion-laughs document is rejected instead of exhausting memory - YAML 1.2 core schema resolution — null/bool/int (decimal, octal, hex)/float including
.infand.nan/string, plus explicit!!str,!!int,!!binary… tags NodekeepsKind,Tag,Value,Anchor,Style,Line/ColumnandHeadComment/LineComment/FootComment, withNode.Decode/Node.Encodefor comment-preserving round-tripsTypeErrorcollects per-field decode failures instead of aborting the whole decodeMarshaler/Unmarshalerplusencoding.TextMarshaler/TextUnmarshalersupport- Wrapped sentinel errors —
ErrSyntax,ErrUnknownAnchor,ErrAliasBomb,ErrTypeMismatch— all matchable witherrors.Is, and syntax errors carry line and column - Zero dependencies — pure Go standard library, no cgo, nothing to audit but the toolchain