Skip to content
Golang config file parse module
Go
Branch: master
Clone or download
Latest commit 13cb73d Nov 15, 2016
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples add comment support Apr 14, 2015
.gitignore Initial commit Jan 18, 2014
README.md
conf.go fix a comment error Nov 10, 2016
conf_test.go add comment support Apr 14, 2015
doc.go fix examples bug Nov 20, 2015

README.md

Terry-Mao/goconf

Terry-Mao/goconf is an configuration file parse module.

Requeriments

  • Go 1.2 or higher

Installation

Just pull Terry-Mao/goconf from github using go get:

# download the code
$ go get -u github.com/Terry-Mao/goconf

Usage

package main                                                                   
                                                                               
import (                                                                       
    "fmt"                                                                      
    "github.com/Terry-Mao/goconf"                                              
    "time"
)                                                                              

type TestConfig struct {
	ID     int           `goconf:"core:id"`
	Col    string        `goconf:"core:col"`
	Ignore int           `goconf:"-"`
	Arr    []string      `goconf:"core:arr:,"`
	Test   time.Duration `goconf:"core:t_1:time"`
	Buf    int           `goconf:"core:buf:memory"`
    M      map[int]string`goconf:"core:m:,"`
}
                                                                               
func main() {                                                                  
    conf := goconf.New()                                                       
    if err := conf.Parse("./examples/conf_test.txt"); err != nil {             
        panic(err)                                                             
    }                                                                          
    core := conf.Get("core")                                                   
    if core == nil {                                                           
        panic("no core section")                                               
    }                                                                          
    id, err := core.Int("id")                                                  
    if err != nil {                                                            
        panic(err)                                                             
    }                                                                          
    fmt.Println(id)                                                            
    tf := &TestConfig{}
    if err := conf.Unmarshal(tf); err != nil {
        panic(err)
    }
    fmt.Println(tf.ID)
} 

Examples

# configuration examples
# this is comment, goconf will ignore it.

# this is the section name
[core]

# a key-value config which key is test and value is 1
test 1

# one mb
test1 1mb

# one second
test2 1s

# boolean
test3 true

# arr
arr hello,the,world

# map
m 1=hello,2=the,3=world

Documentation

Read the Terry-Mao/goconf documentation from a terminal

$ godoc github.com/Terry-Mao/goconf -http=:6060

Alternatively, you can goconf online.

You can’t perform that action at this time.