Toml

TOML parser for Golang with reflection.
Alternatives To Toml
Project NameStarsDownloadsRepos Using ThisPackages Using ThisMost Recent CommitTotal ReleasesLatest ReleaseOpen IssuesLicenseLanguage
Toml4,1046,1632 months ago47June 25, 202213mitGo
TOML parser for Golang with reflection.
Ffjson2,6202423563 years ago2February 06, 201557apache-2.0Go
faster JSON serialization for Go
Schema1,1573445533 months ago6May 20, 20219bsd-3-clauseGo
Package gorilla/schema fills a struct with form values.
Csvutil783367a month ago22March 20, 2022mitGo
csvutil provides fast and idiomatic mapping between CSV and Go (golang) values.
Jingo718252 years ago7April 11, 2021apache-2.0Go
This package provides the ability to encode golang structs to a buffer as JSON very quickly.
Json Rust3353671493 years ago53March 17, 202037otherRust
JSON implementation in Rust
Toml2883414978 months ago6July 30, 20216mitGo
TOML parser and encoder library for Golang
Toolbox1899322 hours ago84March 14, 20225apache-2.0Go
Toolbox - go utility library
Jettison14123 months ago5March 21, 20221mitGo
Highly configurable, fast JSON encoder for Go
Qs66
4 months ago8June 09, 2021mitGo
Go module for encoding structs into URL query parameters
Alternatives To Toml
Select To Compare


Alternative Project Comparisons
Readme

TOML stands for Tom's Obvious, Minimal Language. This Go package provides a reflection interface similar to Go's standard library json and xml packages.

Compatible with TOML version v1.0.0.

Documentation: https://godocs.io/github.com/BurntSushi/toml

See the releases page for a changelog; this information is also in the git tag annotations (e.g. git show v0.4.0).

This library requires Go 1.13 or newer; add it to your go.mod with:

% go get github.com/BurntSushi/[email protected]

It also comes with a TOML validator CLI tool:

% go install github.com/BurntSushi/toml/cmd/[email protected]
% tomlv some-toml-file.toml

Examples

For the simplest example, consider some TOML file as just a list of keys and values:

Age = 25
Cats = [ "Cauchy", "Plato" ]
Pi = 3.14
Perfection = [ 6, 28, 496, 8128 ]
DOB = 1987-07-05T05:45:00Z

Which can be decoded with:

type Config struct {
	Age        int
	Cats       []string
	Pi         float64
	Perfection []int
	DOB        time.Time
}

var conf Config
_, err := toml.Decode(tomlData, &conf)

You can also use struct tags if your struct field name doesn't map to a TOML key value directly:

some_key_NAME = "wat"
type TOML struct {
    ObscureKey string `toml:"some_key_NAME"`
}

Beware that like other decoders only exported fields are considered when encoding and decoding; private fields are silently ignored.

Using the Marshaler and encoding.TextUnmarshaler interfaces

Here's an example that automatically parses values in a mail.Address:

contacts = [
    "Donald Duck <[email protected]>",
    "Scrooge McDuck <[email protected]>",
]

Can be decoded with:

// Create address type which satisfies the encoding.TextUnmarshaler interface.
type address struct {
	*mail.Address
}

func (a *address) UnmarshalText(text []byte) error {
	var err error
	a.Address, err = mail.ParseAddress(string(text))
	return err
}

// Decode it.
func decode() {
	blob := `
		contacts = [
			"Donald Duck <[email protected]>",
			"Scrooge McDuck <[email protected]>",
		]
	`

	var contacts struct {
		Contacts []address
	}

	_, err := toml.Decode(blob, &contacts)
	if err != nil {
		log.Fatal(err)
	}

	for _, c := range contacts.Contacts {
		fmt.Printf("%#v\n", c.Address)
	}

	// Output:
	// &mail.Address{Name:"Donald Duck", Address:"[email protected]"}
	// &mail.Address{Name:"Scrooge McDuck", Address:"[email protected]"}
}

To target TOML specifically you can implement UnmarshalTOML TOML interface in a similar way.

More complex usage

See the _example/ directory for a more complex example.

Popular Struct Projects
Popular Encoder Projects
Popular Computer Science Categories
Related Searches

Get A Weekly Email With Trending Projects For These Categories
No Spam. Unsubscribe easily at any time.
Golang
Struct
Encoder
Reflection
Toml