CalcCafe

YAML to Go

Paste any YAML document and get clean, idiomatic Go struct definitions with inferred types and yaml tags.

Example

Input YAML:

name: widget
version: 3
owner:
 firstName: Jo
 id: 5

Output Go:

type Root struct {
	Name  string `yaml:"name"`
	Version int  `yaml:"version"`
	Owner  Owner `yaml:"owner"`
}

type Owner struct {
	FirstName string `yaml:"firstName"`
	ID    int  `yaml:"id"`
}

How it works

The tool parses your YAML with js-yaml, walks the resulting value to infer Go types, and emits named structs (root type "Root") with proper yaml struct tags. Nested objects become their own structs and arrays of objects are merged so every key appears.

Good to know

YAML to Go takes a YAML document and generates ready-to-use Go struct definitions, complete with inferred field types and yaml:"..." struct tags that match your original keys. It is aimed at Go developers who need to unmarshal a config file, a Kubernetes manifest, a CI pipeline definition, or an API response into typed structs without hand-writing the boilerplate. Everything runs locally in your browser, so you can paste internal or sensitive config without it leaving your machine.

Reach for it whenever you have a known YAML shape and want a starting point for gopkg.in/yaml.v3 (or v2) decoding. Typical moments: scaffolding a new service that reads a settings file, mapping a third-party YAML schema you do not control, or quickly checking how a nested structure would translate into Go types. It saves the tedious back-and-forth of naming structs, capitalizing fields for export, and writing tags by hand.

Reading the output is straightforward: the top-level type is always called Root, and every nested mapping becomes its own named struct referenced from its parent. Field names are exported (capitalized) and common initialisms like ID, URL, and API are upper-cased, while the tag preserves the exact YAML key so decoding still works. Watch the type column to confirm the inference matched your intent, since whole numbers become int, decimals become float64, and missing or null values become interface{}.

One practical caveat: types are inferred from the literal sample you paste, so a single example may not capture every variation in real data. If a numeric field is sometimes absent or sometimes a decimal, give a representative sample (the tool merges keys across array elements), and review fields typed as interface{} or int that you may want to widen to pointers or float64 for optional or fractional values.

Frequently asked questions

How are types inferred from YAML?
Each value is mapped to a Go type: integers become int, decimals float64, true/false bool, text string, mappings become named structs, and sequences become slices. Null or empty values fall back to interface{}.
How does it handle arrays of objects with different keys?
It merges the keys of every object in the array before generating a single element struct, so all fields seen across the list appear in one type definition rather than producing duplicate structs.
Is my data uploaded anywhere?
No — this tool runs entirely in your browser. Your input never leaves your device and it works offline once loaded.
Is it free?
Yes, completely free with no sign-up and no limits.

People also ask

Does YAML to Go add omitempty to the struct tags?
No. The generated tags contain only the original YAML key, such as yaml:"name". If you want omitempty or other tag options, add them manually after pasting the output.
Why is my top-level type always named Root?
The tool uses Root as the fixed name for the outermost struct, then derives nested struct names from their parent keys. You can rename Root to anything you like once the code is generated.
How does it handle YAML keys with dashes or special characters?
It strips non-alphanumeric characters and converts the key to a CamelCase exported Go field name, while keeping the exact original key inside the yaml tag so decoding still matches. A key starting with a digit gets an F prefix to stay a valid identifier.
Which Go YAML library does the output work with?
The yaml struct tags are compatible with the common gopkg.in/yaml.v2 and yaml.v3 packages, which both read tags in the yaml:"key" format.
What happens to empty arrays or null values in my YAML?
An empty sequence becomes []interface{}, and a null or empty value becomes interface{}, since the tool cannot infer a concrete type without data. Provide a populated example if you want a specific element type.
Can it convert a YAML file whose top level is a list instead of a map?
Yes. If the document is a sequence, it generates the element struct and defines Root as a slice of that type, for example type Root []Item.
Does the tool detect duplicate nested struct names?
When two different nested objects would produce the same struct name, it appends a number to keep each definition unique rather than overwriting one. Review these to merge or rename them if they actually represent the same shape.

Related tools