YAML Parser Implementation in Go
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓YAML Parser runs in O(n) time and O(n) space, in Go as in every other language.
- ✓No YAML in the standard library, which is a deliberate omission given that Go ships JSON and XML.
- ✓Define a struct with yaml tags and unmarshal into it. Use KnownFields on a Decoder to reject keys the struct does not declare, which is the direct equivalent of DisallowUnknownFields in encoding/json.
- ✓The Go trap: Version 2 of the package implemented YAML 1.1 and therefore treated yes, no, on and off as booleans; version 3 follows the YAML 1.2 core schema and treats them as strings.
- ✓The package is pure Go and slower than encoding/json. As with every configuration parser, this matters only if you are parsing per request rather than at boot.
What YAML Parser actually is
YAML Parser in Go: what you actually reach for
Go implementation
package config
import (
"bytes"
"fmt"
"gopkg.in/yaml.v3"
)
type AppConfig struct {
// Without a tag the default key is the LOWERCASED field name
// ("port"), not the exact field name, unlike encoding/json.
Port int `yaml:"port"`
Host string `yaml:"host"`
Features []string `yaml:"features"`
}
// Strict: a key the struct does not declare is an error, so a typo in a
// config file fails loudly instead of leaving the default in place.
func Load(src []byte) (AppConfig, error) {
dec := yaml.NewDecoder(bytes.NewReader(src))
dec.KnownFields(true) // the equivalent of json's DisallowUnknownFields
var cfg AppConfig
if err := dec.Decode(&cfg); err != nil {
return AppConfig{}, fmt.Errorf("parse config: %w", err)
}
return cfg, nil
}
// THE UPGRADE TRAP. yaml.v2 implemented YAML 1.1, where yes/no/on/off are
// booleans. yaml.v3 follows YAML 1.2 core, where they are STRINGS. The same
// file changes meaning across the version bump, silently:
//
// feature: on
// v2 -> true (bool)
// v3 -> "on" (string)
//
// Quote the value and the meaning is fixed under both.Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Go-specific trap
Performance in Go, beyond the O(n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Go type or library should I use for YAML Parser?
No YAML in the standard library, which is a deliberate omission given that Go ships JSON and XML. The package everyone uses is gopkg.in/yaml.v3, and its version boundary matters as much as js-yaml version 4 does.
What is the most common Go mistake when implementing YAML Parser?
Version 2 of the package implemented YAML 1.1 and therefore treated yes, no, on and off as booleans; version 3 follows the YAML 1.2 core schema and treats them as strings. That means the same configuration file changes meaning across a dependency upgrade, silently, in the direction that a field which used to be a boolean is now the string on. Nothing errors, because the target field will often be an interface value or a string. The other item to know is that struct fields must be exported to be visible to the unmarshaller and, unlike encoding/json, the default key is the lowercased field name rather than the exact name, so omitting the tag gives you a key you did not expect.
How does YAML Parser actually perform in Go?
The package is pure Go and slower than encoding/json. As with every configuration parser, this matters only if you are parsing per request rather than at boot. Time complexity is O(n) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialBefore paying full price for a tool, check the student deals on AI and developer tools listed on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.