TFT

TOML to Go Struct Converter

Convert TOML configuration files into Go structs ready for unmarshaling. This tool generates idiomatic Go code with TOML struct tags, saving you time when working with config files in Go projects. It handles nested tables and arrays correctly.

TOML to Go Struct Converter

How the TOML to Go Struct Converter Works

This tool converts TOML configuration files into Go structs with proper TOML tags. Paste your TOML content and get idiomatic Go code ready for unmarshaling configuration data in your Go applications.

The converter analyzes TOML tables, arrays, and values to generate corresponding Go types. Strings become string, integers become int or int64, booleans become bool. Nested tables become nested structs with appropriate field tags.

Generated code includes TOML struct tags for seamless unmarshaling using encoding libraries like BurntSushi/toml or Pelletier/go-toml. Copy the output directly into your Go project and start loading configuration files immediately.

When You'd Actually Use This

Building Go applications with config files

Your Go app needs configuration. Define settings in TOML, generate structs, and use toml.Unmarshal() to load settings. Type-safe configuration without manual parsing.

Creating CLI tools

CLI tools often need config files for default settings. Generate structs from your TOML config template and load user configurations effortlessly.

Migrating from JSON to TOML

Switching your Go project from JSON to TOML config? Regenerate your structs with TOML tags instead of JSON tags. The structure stays similar.

Documenting configuration schemas

Generated structs serve as living documentation of your config schema. Team members can see available options by reading the struct definitions.

Validating configuration structure

Generate structs to verify your TOML design makes sense. If the generated Go code looks awkward, your TOML structure might need refinement.

Learning Go struct tags

New to Go? See how TOML maps to Go types. Study the generated tags to understand how Go serialization libraries work with struct field annotations.

What to Know Before Using

TOML types map to specific Go types.TOML strings → string, integers → int64, floats → float64, booleans → bool, arrays → slices, tables → structs. The converter handles these mappings automatically.

Struct tags enable unmarshaling.Tags like `toml:"database_host"` tell the TOML library which field receives which config value. Don't remove these tags or unmarshaling will fail.

Nested tables become nested structs.TOML's hierarchical structure translates to nested Go structs. Each table level becomes a new struct type with its own fields and tags.

Array types are inferred from content.TOML arrays of integers become []int64, arrays of strings become []string. Mixed-type arrays become []interface but TOML discourages mixing.

Pro tip: Add comments to your TOML file before generating. Some converters preserve comments as Go doc comments, making your structs self-documenting.

Common Questions

Which TOML library should I use?

BurntSushi/toml is the most popular. Pelletier/go-toml offers faster parsing. Both work with generated structs. Choose based on your project's needs.

How do I handle optional fields?

Use pointer types (*string, *int) for optional fields. Nil means not specified. Or use struct tags with omitempty for marshaling behavior.

Can I add custom methods to generated structs?

Yes, copy the generated struct to your code file, then add methods. Consider the struct as a starting point—extend it with validation or helper methods.

What about datetime values?

TOML datetimes map to time.Time in Go. The generated struct uses time.Time fields. TOML libraries handle parsing ISO 8601 datetime strings automatically.

How do I validate config values?

Add validation methods to your structs. Check ranges, required fields, and formats after unmarshaling. Consider using go-playground/validator for tag-based validation.

Can I generate structs for partial TOML?

Yes, only include the tables you need. Your Go struct can represent a subset of the TOML file. Unmarshal will populate only matching fields.

What about inline tables?

Inline tables { key = value } are treated like regular tables. They generate nested structs. The TOML syntax difference doesn't affect Go struct generation.