YAML to Go Struct
Generate Go structs with yaml tags from a YAML config file, ready for gopkg.in/yaml.v3, Viper or mapstructure.
package main
type Config struct {
Server Server `yaml:"server"`
Database Database `yaml:"database"`
FeatureFlags []string `yaml:"feature_flags"`
LogLevel string `yaml:"log_level"`
}
type Server struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
ReadTimeout string `yaml:"read_timeout"`
}
type Database struct {
URL string `yaml:"url"`
MaxOpenConns int `yaml:"max_open_conns"`
Replicas []Replica `yaml:"replicas"`
}
type Replica struct {
Host string `yaml:"host"`
Weight int `yaml:"weight"`
ReadOnly bool `yaml:"read_only,omitempty"`
}
Types are inferred from this sample. Whole numbers become int (int64 when too large for 32 bits), decimals float64, and values that are null everywhere become any.
About this tool
Go services usually load their settings from a YAML file into a config struct, and Kubernetes manifests, CI pipelines and Helm values are YAML too. Writing the matching struct by hand is tedious and easy to get subtly wrong. Paste the YAML here and get gofmt-formatted Go structs with yaml tags, one named struct per nested mapping.
You can also add mapstructure tags for Viper, json tags or toml tags. Lists of mappings are merged, so a key that appears in only some list items gets omitempty, and snake_case or kebab-case keys become idiomatic Go names such as MaxOpenConns or ReadOnly. The YAML is parsed in your browser and never uploaded.
How to use YAML to Go Struct
- Paste YAML or open a .yaml or .yml file. Only the first document is used if the file contains several.
- Set the struct name (Config by default) and the package.
- Choose the tags: yaml for gopkg.in/yaml.v3, mapstructure for Viper, and json or toml if needed.
- Copy the result or download it as types.go.
Examples
A server config for Viper
server: host: 0.0.0.0 port: 8080 read_timeout: 30s
type Server struct { Host string `yaml:"host" mapstructure:"host"`; Port int; ReadTimeout string } A list of mappings
replicas:
- host: a
weight: 2
- host: b
weight: 1
read_only: true Replicas []Replica; type Replica struct { Host string; Weight int; ReadOnly bool `yaml:"read_only,omitempty"` } Values that look like numbers
port: "8080" enabled: yes version: 1.10
Port string; Enabled string; Version float64
version: 1.10 is the number 1.1. Quote it ("1.10") if it is a version string.
YAML types to watch out for
- This tool follows YAML 1.2, where only true and false are booleans. yes, no, on and off are strings, so quote or change them if you mean a boolean.
- Unquoted numbers keep their numeric type: 1.10 is the float 1.1 and 0755 may not be what you expect. Quote versions, ZIP codes and phone numbers.
- Durations such as 30s are strings. If you load the config with Viper, you can change the field type to time.Duration and let a decode hook parse it.
- Anchors and aliases (&default, <<: *default) are expanded before the struct is generated.
yaml or mapstructure tags?
gopkg.in/yaml.v3 reads the yaml tag. Viper first loads the file into a map and then decodes it with mapstructure, so it reads the mapstructure tag and, without one, matches field names case-insensitively, which does not cover snake_case keys such as read_timeout. Emit both tags if you are not sure which library will read the struct.
Frequently asked questions
Everything you need to know about this tool and data security.
Is my YAML uploaded?
No. It is parsed and converted in your browser, so configs with hosts or credentials stay on your device.
What about files with several documents separated by ---?
Only the first document is used. Paste each document separately to generate types for the others, as Kubernetes manifests often contain several kinds.
Why is a field typed as any?
Its value is empty (null) everywhere in your sample, so its type cannot be inferred. Add an example value or set the type by hand.
Can I generate types for a Kubernetes manifest?
You can, but the official Go types in k8s.io/api are complete and versioned, so prefer them for standard resources and use this tool for your own CRDs or Helm values.
Is my data safe when using this tool?
Yes, 100% safe. This tool operates entirely inside your web browser using client-side JavaScript and WebAssembly. Your data never touches any external server.
Related Tools
Generate Go struct definitions with json tags from a JSON sample, including nested types, slices, optional and nullable fields.
Convert bidirectionally between JSON and YAML in real time.
Turn CREATE TABLE statements from MySQL, PostgreSQL or SQLite into Go structs with json, db and GORM tags.
Featured Projects tech-fun.net
Explore more AI & productivity tools
Nihongo GameBox
Master Japanese typing, Kana, and JLPT N5–N1 vocabulary through interactive retro arcade games.
ResumeGPT
Tailors CVs and cover letters to every job opportunity with a multi-agent LLM pipeline, job tracking, and scheduled discovery.
CollectMind
Turns your bookmarks into a searchable AI knowledge base — entirely on-device, powered by Chrome's built-in Gemini Nano.