# go-toml v2
Go library for the [TOML](https://toml.io/en/) format.
This library supports [TOML v1.0.0](https://toml.io/en/v1.0.0).
[🐞 Bug Reports](https://github.com/pelletier/go-toml/issues)
[💬 Anything else](https://github.com/pelletier/go-toml/discussions)
## Documentation
Full API, examples, and implementation notes are available in the Go
documentation.
[](https://pkg.go.dev/github.com/pelletier/go-toml/v2)
## Import
```go
import "github.com/pelletier/go-toml/v2"
```
See [Modules](#Modules).
## Features
### Stdlib behavior
As much as possible, this library is designed to behave similarly as the
standard library's `encoding/json`.
### Performance
While go-toml favors usability, it is written with performance in mind. Most
operations should not be shockingly slow. See [benchmarks](#benchmarks).
### Strict mode
`Decoder` can be set to "strict mode", which makes it error when some parts of
the TOML document was not present in the target structure. This is a great way
to check for typos. [See example in the documentation][strict].
[strict]: https://pkg.go.dev/github.com/pelletier/go-toml/v2#example-Decoder.DisallowUnknownFields
### Contextualized errors
When most decoding errors occur, go-toml returns [`DecodeError`][decode-err]),
which contains a human readable contextualized version of the error. For
example:
```
2| key1 = "value1"
3| key2 = "missing2"
 | ~~~~ missing field
4| key3 = "missing3"
5| key4 = "value4"
```
[decode-err]: https://pkg.go.dev/github.com/pelletier/go-toml/v2#DecodeError
### Local date and time support
TOML supports native [local date/times][ldt]. It allows to represent a given
date, time, or date-time without relation to a timezone or offset. To support
this use-case, go-toml provides [`LocalDate`][tld], [`LocalTime`][tlt], and
[`LocalDateTime`][tldt]. Those types can be transformed to and from `time.Time`,
making them convenient yet unambiguous structures for their respective TOML
representation.
[ldt]: https://toml.io/en/v1.0.0#local-date-time
[tld]: https://pkg.go.dev/github.com/pelletier/go-toml/v2#LocalDate
[tlt]: https://pkg.go.dev/github.com/pelletier/go-toml/v2#LocalTime
[tldt]: https://pkg.go.dev/github.com/pelletier/go-toml/v2#LocalDateTime
## Getting started
Given the following struct, let's see how to read it and write it as TOML:
```go
type MyConfig struct {
      Version int
      Name    string
      Tags    []string
}
```
### Unmarshaling
[`Unmarshal`][unmarshal] reads a TOML document and fills a Go structure with its
content. For example:
```go
doc := `
version = 2
name = "go-toml"
tags = ["go", "toml"]
`
var cfg MyConfig
err := toml.Unmarshal([]byte(doc), &cfg)
if err != nil {
      panic(err)
}
fmt.Println("version:", cfg.Version)
fmt.Println("name:", cfg.Name)
fmt.Println("tags:", cfg.Tags)
// Output:
// version: 2
// name: go-toml
// tags: [go toml]
```
[unmarshal]: https://pkg.go.dev/github.com/pelletier/go-toml/v2#Unmarshal
### Marshaling
[`Marshal`][marshal] is the opposite of Unmarshal: it represents a Go structure
as a TOML document:
```go
cfg := MyConfig{
      Version: 2,
      Name:    "go-toml",
      Tags:    []string{"go", "toml"},
}
b, err := toml.Marshal(cfg)
if err != nil {
      panic(err)
}
fmt.Println(string(b))
// Output:
// Version = 2
// Name = 'go-toml'
// Tags = ['go', 'toml']
```
[marshal]: https://pkg.go.dev/github.com/pelletier/go-toml/v2#Marshal
## Unstable API
This API does not yet follow the backward compatibility guarantees of this
library. They provide early access to features that may have rough edges or an
API subject to change.
### Parser
Parser is the unstable API that allows iterative parsing of a TOML document at
the AST level. See https://pkg.go.dev/github.com/pelletier/go-toml/v2/unstable.
## Benchmarks
Execution time speedup compared to other Go TOML libraries:
    
        | Benchmark | go-toml v1 | BurntSushi/toml | 
|---|
    
    
        | Marshal/HugoFrontMatter-2 | 1.9x | 1.9x | 
        | Marshal/ReferenceFile/map-2 | 1.7x | 1.8x | 
        | Marshal/ReferenceFile/struct-2 | 2.2x | 2.5x | 
        | Unmarshal/HugoFrontMatter-2 | 2.9x | 2.9x | 
        | Unmarshal/ReferenceFile/map-2 | 2.6x | 2.9x | 
        | Unmarshal/ReferenceFile/struct-2 | 4.4x | 5.3x | 
     
See more
The table above has the results of the most common use-cases. The table below
contains the results of all benchmarks, including unrealistic ones. It is
provided for completeness.
    
        | Benchmark | go-toml v1 | BurntSushi/toml | 
|---|
    
    
        | Marshal/SimpleDocument/map-2 | 1.8x | 2.9x | 
        | Marshal/SimpleDocument/struct-2 | 2.7x | 4.2x | 
        | Unmarshal/SimpleDocument/map-2 | 4.5x | 3.1x | 
        | Unmarshal/SimpleDocument/struct-2 | 6.2x | 3.9x | 
        | UnmarshalDataset/example-2 | 3.1x | 3.5x | 
        | UnmarshalDataset/code-2 | 2.3x | 3.1x | 
        | UnmarshalDataset/twitter-2 | 2.5x | 2.6x | 
        | UnmarshalDataset/citm_catalog-2 | 2.1x | 2.2x | 
        | UnmarshalDataset/canada-2 | 1.6x | 1.3x | 
        | UnmarshalDataset/config-2 | 4.3x | 3.2x | 
        | [Geo mean] | 2.7x | 2.8x | 
     
This table can be generated with ./ci.sh benchmark -a -html.