/

YAML to Go

Processed Client Side

Generate idiomatic Go structs with tags from a YAML document, with nested types inferred.

YAML · 10 lines
10 lines · 103 chars
Length: 103Lines: 10Size: 103 BytesCursor: 1:1
Go
Go
goLength: 304Lines: 14Size: 304 BytesCursor: 1:1

Bookmark this tool now — skip the search next time you need it.

About YAML to Go

This tool runs entirely in your browser. Whatever you paste is processed on your own device and is never uploaded, logged, or sent to any server.

The YAML to Go converter turns a YAML document into idiomatic Go structs, ready to paste into a package and unmarshal into. It infers the type of every field from the sample values, gives nested maps their own named structs, merges the shapes of objects inside a list so entries with slightly different keys still produce one type, and applies Go naming conventions including the initialisms the style guide insists on, so user_id becomes UserID rather than UserId. Choose yaml tags, json tags, or both, name the root struct, and copy the result. Parsing and code generation both happen in your browser, so configuration files that often hold secrets never leave your machine.

Key features

  • Idiomatic Go structs generated from any YAML document
  • yaml tags, json tags, or both emitted on every field
  • Nested maps become their own named structs and are referenced as pointers, so a missing section unmarshals as nil
  • Objects inside a list are merged into one struct, so entries with extra keys still fit a single type
  • Go naming with initialism handling: api_url becomes APIURL, http_ttl becomes HTTPTTL
  • Types inferred from the values, including slices and interface{} where a key is mixed or always null
  • Configurable root struct name, with collisions resolved automatically
  • Field and type columns aligned the way gofmt would align them
  • Highlighted YAML in and highlighted Go out, running entirely in your browser

How to use it

  1. Paste a YAML document into the left pane.
  2. Type the name you want for the top-level struct.
  3. Choose whether to emit yaml tags, json tags, or both.
  4. Click Convert to generate the structs.
  5. Copy the Go into your package and unmarshal into it.

Tips & common mistakes

  • Give the richest sample you have. Fields absent from the sample cannot be generated, and a key that is null everywhere can only become interface{}.
  • Whole numbers become int and anything with a decimal point becomes float64, so write 1.0 rather than 1 where the field really is a float.
  • A key whose values have different types across the sample resolves to interface{}. Splitting it into two fields is almost always the better fix.
  • Pick yaml plus json tags when the same struct serves both a config file and a JSON API.
  • Struct names are a starting point, not a decision. Rename them to fit your domain freely; the tags carry the wire format, so the YAML keeps matching.
  • Add omitempty by hand where you need it. The generator emits plain tags so nothing is silently dropped on marshal.
  • Only one document is read at a time. If your file uses --- separators, convert each document on its own.

Related tools

Browse all 4 Other Converters tools

Frequently asked questions

10

Paste a YAML document into the left pane, type the name you want for the top-level struct, choose which tags to emit, and press Convert. The generated structs appear on the right, ready to copy into a package.

Yes. Pick yaml tags, json tags, or both from the selector. Combined tags are the right choice when the same struct serves a YAML config file and a JSON API.

A nested map becomes its own named struct, referenced from the parent as a pointer so an absent section unmarshals as nil rather than a zero value. A list becomes a Go slice whose element type is inferred from the values inside it.

Their shapes are merged into a single struct containing every key seen, so a list where only some entries carry an optional field still produces one clean type instead of several.

That happens when a key holds different types across the sample, or when it is null everywhere so there is nothing to infer from. Splitting the key into two fields, or supplying a sample with a real value, gives you a concrete type.

Whole numbers become int and anything with a decimal point becomes float64. If a field really is a float, write it as 1.0 in the sample so the generated type matches what your data will contain.

Yes. Anchors, aliases, and merge keys are resolved while the YAML is parsed, so the generated struct reflects the expanded document. Multi-document files separated by --- are not read as a set, so convert each document on its own.

Yes. Keys are converted to exported PascalCase names and common initialisms are upper-cased the way the Go style guide requires, so user_id becomes UserID and api_url becomes APIURL. The original key is preserved in the struct tag.

No, the generator emits plain tags. Add omitempty yourself on the fields where you want empty values left out of the marshalled output, since applying it everywhere silently drops legitimate zero values.

No. Parsing and code generation both run in your browser, which matters here because YAML config files so often hold credentials.