Package toml

import "github.com/BurntSushi/toml-backup"
Overview
Index
Examples
Subdirectories

Overview ▾

Package toml provides facilities for decoding TOML configuration files via reflection.

Specification: https://github.com/mojombo/toml

Use github.com/BurntSushi/toml/tomlv to check whether a file is valid TOML or not, with helpful error messages.

func Decode

func Decode(data string, v interface{}) error

Decode will decode the contents of `data` in TOML format into a pointer `v`.

TOML hashes correspond to Go structs or maps. (Dealer's choice. They can be used interchangeably.)

TOML datetimes correspond to Go `time.Time` values.

All other TOML types (float, string, int, bool and array) correspond to the obvious Go types.

TOML keys can map to either keys in a Go map or field names in a Go struct. The special `toml` struct tag may be used to map TOML keys to struct fields that don't match the key name exactly. (See the example.) A case insensitive match to struct names will be tried if an exact match can't be found.

The mapping between TOML values and Go values is loose. That is, there may exist TOML values that cannot be placed into your representation, and there may be parts of your representation that do not correspond to TOML values.

This decoder will not handle cyclic types. If a cyclic type is passed, `Decode` will not terminate.

Example

Code:

var tomlBlob = `
# Some comments.
[alpha]
ip = "10.0.0.1"

[alpha.config]
Ports = [ 8001, 8002 ]
Location = "Toronto"
Created = 1987-07-05T05:45:00Z

[beta]
ip = "10.0.0.2"

[beta.config]
Ports = [ 9001, 9002 ]
Location = "New Jersey"
Created = 1887-01-05T05:55:00Z
`

type serverConfig struct {
    Ports    []int
    Location string
    Created  time.Time
}

type server struct {
    IP     string       `toml:"ip"`
    Config serverConfig `toml:"config"`
}

type servers map[string]server

var config servers
if err := Decode(tomlBlob, &config); err != nil {
    log.Fatal(err)
}

for _, name := range []string{"alpha", "beta"} {
    s := config[name]
    fmt.Printf("Server: %s (ip: %s) in %s created on %s\n",
        name, s.IP, s.Config.Location,
        s.Config.Created.Format("2006-01-02"))
    fmt.Printf("Ports: %v\n", s.Config.Ports)
}

Output:

Server: alpha (ip: 10.0.0.1) in Toronto created on 1987-07-05
Ports: [8001 8002]
Server: beta (ip: 10.0.0.2) in New Jersey created on 1887-01-05
Ports: [9001 9002]

func DecodeFile

func DecodeFile(fpath string, v interface{}) error

DecodeFile is just like Decode, except it will automatically read the contents of the file at `fpath` and decode it for you.

func DecodeReader

func DecodeReader(r io.Reader, v interface{}) error

DecodeReader is just like Decode, except it will consume all bytes from the reader and decode it for you.

Subdirectories

Name      Synopsis
..
toml-test-go     
tomlv