Go bindings for YAMLStar - a pure YAML 1.2 loader implemented in Clojure.
- YAML 1.2 Spec Compliance: 100% compliant with YAML 1.2 core schema
- Pluggable YAML: Select parser and emitter implementations per call
- Pure Go: Generated by Gloat/Glojure with no cgo dependency
- Simple API: Load YAML documents with a single function call
- Multi-Document Support: Load multiple YAML documents from a single string
- Thread Safe: Safe for concurrent use without lifecycle management
go get github.com/yaml/yamlstar-go@latestpackage main
import (
"fmt"
"log"
"github.com/yaml/yamlstar-go"
)
func main() {
// Load a simple YAML string
data, err := yamlstar.Load("key: value")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%v\n", data) // map[key:value]
}import "github.com/yaml/yamlstar-go"
// Strings
data, _ := yamlstar.Load("hello") // "hello"
// Integers (returned as float64)
data, _ := yamlstar.Load("42") // float64(42)
// Floats
data, _ := yamlstar.Load("3.14") // 3.14
// Booleans
data, _ := yamlstar.Load("true") // true
data, _ := yamlstar.Load("false") // false
// Null
data, _ := yamlstar.Load("null") // nil// Mappings (map[string]any)
data, _ := yamlstar.Load(`
name: Alice
age: 30
city: Seattle
`)
// map[string]any{"name": "Alice", "age": float64(30), "city": "Seattle"}
// Sequences ([]any)
data, _ := yamlstar.Load(`
- apple
- banana
- orange
`)
// []any{"apple", "banana", "orange"}
// Flow style
data, _ := yamlstar.Load("[a, b, c]")
// []any{"a", "b", "c"}// Load all documents from a multi-document YAML string
docs, err := yamlstar.LoadAll(`---
name: Document 1
---
name: Document 2
---
name: Document 3
`)
// []any{
// map[string]any{"name": "Document 1"},
// map[string]any{"name": "Document 2"},
// map[string]any{"name": "Document 3"},
// }text, err := yamlstar.Dump(map[string]any{"key": "value"})
// "key: value\n"
stream, err := yamlstar.DumpAll([]any{"one", "two"})
// "---\none\n---\ntwo\n"data, err := yamlstar.Load(`invalid: yaml: syntax`)
if err != nil {
fmt.Printf("Error loading YAML: %v\n", err)
}
// Check for specific error types
var yamlErr *yamlstar.YAMLError
if errors.As(err, &yamlErr) {
fmt.Printf("YAML error type: %s\n", yamlErr.Type)
fmt.Printf("YAML error cause: %s\n", yamlErr.Cause)
}// Get library version
version, err := yamlstar.LibVersion()
fmt.Printf("YAMLStar library version: %s\n", version)
// Get binding version constant
fmt.Printf("Go binding version: %s\n", yamlstar.Version)Load a single YAML document.
Parameters:
input: String containing YAML contentopts: Optional functional options (see Options below)
Returns:
- Go value representing the YAML document (nil, bool, float64, string, []any, or map[string]any)
- Error if the YAML is malformed or library not initialized
Load all YAML documents from a multi-document string.
Parameters:
input: String containing one or more YAML documentsopts: Optional functional options (see Options below)
Returns:
- Slice of Go values, one per YAML document
- Error if the YAML is malformed or library not initialized
Serialize one JSON-compatible Go value as YAML.
Serialize JSON-compatible Go values as a multi-document YAML stream.
Select the parser plugin used for loading (see https://yamlstar.org/plugins/):
data, err := yamlstar.Load(
"key: value",
yamlstar.WithPlugin(yamlstar.YAMLParser("reference")),
)Select the YAML emitter plugin used for dumping:
output, err := yamlstar.Dump(
map[string]any{"key": "value"},
yamlstar.WithPlugin(yamlstar.YAMLEmitter("reference")),
)The generated Go runtime includes the reference and go-yaml
implementations and defaults to go-yaml for both plugin APIs.
Get the generated YAMLStar runtime version string.
Returns:
- Version string from the generated runtime
- Error if runtime initialization fails
The version of the Go binding.
Represents an error returned from YAMLStar.
type YAMLError struct {
Cause string // The error cause
Type string // The error type
Message string // Optional detailed message
}# Run all tests
make test
# Run specific test
go test -v -run TestLoadSimpleMapping# Build the package
make build
# Run the example
make example- Go: 1.24 or higher
- CGO: Not required
- System: Any platform supported by Go and Glojure
The package initializes its generated Glojure runtime once and is safe for concurrent calls. There are no handles to close.
MIT License - See License file
Created by Ingy dot Net, inventor of YAML.
YAMLStar is built on the YAML Reference Parser (pure Clojure implementation).
- GitHub: https://github.com/yaml/yamlstar
- YAML Specification: https://yaml.org/spec/1.2/spec.html