Skip to content

laurent22/toml-go

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 

TOML-GO

An easy-to-use Go parser for the Toml format.

This parser was last tested on TOML version 3f4224ecdc4a65fdd28b4fb70d46f4c0bd3700aa

It correctly parses both the example.toml and hard_example.toml files of the official Toml repository.

Usage

See main.go for some examples or the API doc for the full public API. Basically, you create a parser and give it a file or string to parse. You can then access the values using the Get accessors (eg. GetString(), GetInt(), etc.). You can also provide an optional default value to any of these accessors.

var parser toml.Parser
doc := parser.ParseFile("example.toml")

// Or parse a string directly:
// doc := parser.Parse(someTomlString)

// ==================================================
// Get some values:
// ==================================================

fmt.Println(doc.GetString("servers.beta.ip"))
fmt.Println(doc.GetArray("clients.data"))
fmt.Println(doc.GetInt("doesntexist", 123)) // Optionally, a default value can be provided
fmt.Println(doc.GetString("doesntexisteither", "some default"))
fmt.Println(doc.GetFloat("floats.pi"))
fmt.Println(doc.GetBool("database.enabled"))
fmt.Println(doc.GetDate("owner.dob"))

Advanced usage

// ==================================================
// The GetValue() / As<Type>() pattern provides a bit
// more flexibility but is more verbose
// ==================================================

var value toml.Value
var ok bool

// Get a string:

value, ok = doc.GetValue("servers.beta.ip")
if !ok { // Optionally, you can check if the value exists or not
  panic("value doesn't exist")
} else {
  fmt.Println(value)
}

// Get an array

value, _ = doc.GetValue("clients.data")
fmt.Println(value.AsArray()[0].AsArray()[0].AsString())

// Get an int

value, _ = doc.GetValue("database.connection_max")
fmt.Println(value.AsInt())

// Get a float

value, _ = doc.GetValue("floats.pi")
fmt.Println(value.AsFloat())

// Get a negative float

value, _ = doc.GetValue("floats.minus")
fmt.Println(value.AsFloat())

// Get a boolean

value, _ = doc.GetValue("database.enabled")
fmt.Println(value.AsBool())

// Get a date

value, _ = doc.GetValue("owner.dob")
fmt.Println(value.AsDate())

// Get a section

section, _ := doc.GetSection("owner")
fmt.Println(section)

// Get the title

value, _ = doc.GetValue("title")
fmt.Println(value.AsString())

License

The MIT License (MIT)

Copyright (c) 2013-2014 Laurent Cozic

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Toml parser in Go

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages