Git Product home page Git Product logo

toml-go's Introduction

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())

toml-go's People

Contributors

octopusinvitro avatar laurent22 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.