TFT

Convert YAML to Go Struct

Automatically generate Go struct definitions from your YAML files. This free converter creates type-safe Go code with YAML tags, speeding up development when working with YAML configuration in Go projects.

YAML to Go Struct

Generate Go struct definitions from YAML

How YAML to Go Struct Conversion Works

YAML to Go struct conversion analyzes your YAML structure and generates corresponding Go type definitions. Each YAML key becomes a struct field, nested objects become nested structs, and arrays become slices.

This tool infers Go types from YAML values: strings become string, numbers become int or float64, booleans become bool, and null becomes interface or pointer types. YAML tags are added for proper marshaling.

Here's the process:

  1. YAML is parsed to analyze structure
  2. Types are inferred from values
  3. Go structs are generated with fields
  4. YAML tags are added for marshaling

Example conversion:

YAML Input:
name: John
age: 30
active: true

Go Struct Output:
type Config struct {
  Name   string `yaml:"name"`
  Age    int    `yaml:"age"`
  Active bool   `yaml:"active"`
}

When You'd Actually Use This

Go application configuration

Generate Go structs from YAML config files. Define type-safe configuration structures that match your YAML config files exactly.

API response types

Create Go types for YAML API responses. Generate structs that match expected YAML response formats for type-safe API client code.

Kubernetes operator development

Generate Go types from Kubernetes CRD YAML. Create Go structs that match custom resource definitions for Kubernetes operator development.

Data model definition

Define data models from YAML schemas. Start with YAML data examples, generate Go structs, then implement business logic around them.

Rapid prototyping

Quickly scaffold Go code from YAML examples. Speed up development by auto-generating struct definitions instead of writing them manually.

Documentation generation

Show Go types alongside YAML examples. Generate documentation that includes both YAML examples and corresponding Go type definitions.

What to Know About Go Struct Generation

Type inference is automatic. Types are guessed from YAML values. Numbers without decimals become int, with decimals become float64. Verify types match your needs.

Field names are exported. Generated struct fields start with capital letters (exported in Go). YAML tags preserve the original key names for marshaling.

Nested structures create nested structs. YAML nesting becomes Go struct nesting. Each level generates a separate type definition with appropriate field types.

Arrays become slices. YAML arrays generate Go slices ([]Type). Element types are inferred from the first array element.

Pro tip: Review generated types before using in production. You may want to adjust types (int vs int64), add validation tags, or customize field names.

Common Questions

How are types determined?

Types are inferred from values: text→string, integers→int, decimals→float64, true/false→bool, lists→slice, objects→struct, null→interface.

Can I customize the output?

The generated code is standard Go. Customize by editing the output—add validation tags, change types, or implement custom interfaces as needed.

Does it handle nested structures?

Yes. Nested YAML objects become nested Go structs. Each nesting level generates a separate type with appropriate field definitions.

What about YAML anchors?

Anchors are resolved during parsing. The generated struct reflects the expanded structure, not the anchor references from the original YAML.

Are comments preserved?

YAML comments can become Go comments above fields. This helps document the struct fields with original YAML documentation.

Can I use this with gopkg.in/yaml?

Yes. Generated structs work with both gopkg.in/yaml.v2 and yaml.v3 packages. The yaml tags are compatible with both versions.

Is my code secure?

Yes. All processing happens locally in your browser. Your YAML and generated code never leave your computer. Safe for proprietary code.