How to Convert YAML to Go Structs - Complete Step-by-Step Guide

Step 1

Input Your YAML Configuration

Start by pasting your YAML configuration file. This tool is perfect for converting Kubernetes configs, Docker Compose files, application settings, CI/CD pipelines, or any YAML-based configuration into type-safe Go structs.

•
Paste directly: Copy YAML from configuration files, API responses, or documentation
•
Upload a file: Click "Upload" to select a .yaml or .yml file from your project
•
Try the sample: Click "Sample" to see how a typical application config converts to Go

Example: YAML Configuration Input

Here's a typical YAML configuration for an application:

server:
  host: localhost
  port: 8080
  timeout: 30
  tls:
    enabled: true
    cert_file: /path/to/cert.pem
database:
  host: db.example.com
  port: 5432
  username: admin
  password: secret123
  max_connections: 100
features:
  - authentication
  - logging
  - metrics
Step 2

Automatic Type Detection & Struct Generation

The converter analyzes your YAML structure and automatically generates properly typed Go structs:

•
Types from your values: int, int64 and uint64 by size, float64 for decimals, bool for true/false, string for text, dates and numbers like 00501
•
Nested struct generation: Creates separate struct definitions for nested YAML objects
•
Array handling: Converts YAML sequences to Go slices with proper element types
•
Dual tags: Each field gets json and yaml tags with the original key, so first-name or max_connections still read correctly

Example: Generated Go Struct Output

The YAML config above becomes these type-safe Go structs:

package main

// Read a file with gopkg.in/yaml.v3: var cfg Config; err := yaml.Unmarshal(data, &cfg)

type Config struct {
	Server   Server   `json:"server" yaml:"server"`
	Database Database `json:"database" yaml:"database"`
	Features []string `json:"features" yaml:"features"`
}

type Server struct {
	Host    string `json:"host" yaml:"host"`
	Port    int    `json:"port" yaml:"port"`
	Timeout int    `json:"timeout" yaml:"timeout"`
	TLS     TLS    `json:"tls" yaml:"tls"`
}

type Database struct {
	Host           string `json:"host" yaml:"host"`
	Port           int    `json:"port" yaml:"port"`
	Username       string `json:"username" yaml:"username"`
	Password       string `json:"password" yaml:"password"`
	MaxConnections int    `json:"max_connections" yaml:"max_connections"`
}

type TLS struct {
	Enabled  bool   `json:"enabled" yaml:"enabled"`
	CertFile string `json:"cert_file" yaml:"cert_file"`
}
Step 3

Customize Your Go Code Generation

Fine-tune the output to match your project's coding standards and requirements:

•
Struct name: Customize the root struct name to match your application (e.g., "AppConfig", "Settings")
•
JSON tags: Toggle json struct tags if you need JSON marshaling/unmarshaling
•
YAML tags: Toggle yaml struct tags for use with yaml.v2 or yaml.v3 libraries
•
Field visibility: All fields are exported (capitalized) by default for external package access
Step 4

Export and Integrate Into Your Go Project

Drop the structs into your project and load the file with gopkg.in/yaml.v3:

•
Copy to clipboard: One-click copying to paste into your Go configuration package
•
Download as .go file: Save as a Go source file with proper package declaration
•
Ready for yaml.Unmarshal: Works immediately with yaml.Unmarshal(data, &config)

Frequently Asked Questions

How does the converter pick the Go field types?

From how each value is written in the YAML, not from a rounded JavaScript number. Whole numbers become int, or int64 when they don't fit in 32 bits, so a 19-digit ID like 8901240544102066246 keeps every digit. Values above int64 become uint64, and anything longer than 64 bits becomes string. 14.50 becomes float64, true and false become bool, and quoted text, dates and numbers with a leading zero like 00501 stay string. Every item in a list is looked at, so a key that only some items have becomes a pointer (*int, *bool), and a list that mixes 1 and 2.5 becomes []float64.

Can I use this for Kubernetes config files?

Yes, for the files you actually have. Paste a Deployment, a Helm values.yaml or a Compose file and you get structs for the keys in that file. A file with several documents separated by --- gets one set of structs that covers all of them, and a list at the top level gets a struct for one item. If you're writing an operator against the full Kubernetes API, the official types in k8s.io/api are the better choice, since they cover fields your file leaves out.

What YAML libraries work with the generated structs?

The code is checked against gopkg.in/yaml.v3: the yaml tags hold each original key, so yaml.Unmarshal(data, &cfg) reads every field, including keys like first-name or max_connections that don't look like Go names. gopkg.in/yaml.v2 and github.com/goccy/go-yaml read the same yaml:"..." tags. With json tags switched on, the same structs also work with encoding/json.

How are YAML anchors and aliases handled?

Aliases are followed, so the structs describe the data after every *name has been replaced by what its &anchor points to. The anchors themselves don't appear in the Go code, because yaml.v3 resolves them when it reads the file.

Is my YAML sent anywhere?

No. The YAML is read and the Go code is written in your browser, so nothing is uploaded, and nothing is kept: reload the page and the editor starts empty.

External Resources & Documentation

go-yaml/yaml - YAML v3 for Go

Most popular YAML parsing library for Go with struct marshaling support

YAML 1.2 Specification

Official YAML specification and language reference

gopkg.in/yaml.v3 Package Documentation

Official documentation for the yaml.v3 Go package

Kubernetes Configuration Best Practices

Guide to working with Kubernetes YAML configurations

goccy/go-yaml Alternative Library

High-performance YAML parser with better error messages

Understanding Go Struct Tags

Official Go blog explaining struct tags for YAML and JSON

Kubernetes Client-Go

Go client for Kubernetes with extensive YAML struct examples

Helm Chart Template Guide

Working with YAML templates in Helm for Kubernetes

Go Playground Validator

Struct validation library compatible with YAML-loaded data