Skip to content

Go package for config. Inspired by OpenStack oslo.config library.

License

Notifications You must be signed in to change notification settings

tfcloud-go/config

Repository files navigation

TFCloud Go Config

Go package for config. Inspired by OpenStack oslo.config library.

Usage

Install package

go get github.com/tfcloud-go/config

Register configurations

// conf/server.go

package conf

// ...

var CONF = config.CONF

func init() {
	group := config.NewOptGroup("server")
	CONF.RegisterGroup(group)

	host := config.NewStrOpt("host").WithDefault("127.0.0.1")
	port := config.NewIntOpt("port").WithDefault(8080)
	CONF.RegisterOpts(group, host, port)
}

Parse configurations

// main.go

package main

// ...

var CONF = config.CONF

func main() {
	// ...

	CONF.ParseFile("your_config_file_name.conf")

	// ...
}

Get configuration value

// foo/server.go

package foo

// ...

var CONF = config.CONF

func startServer() {
	host := CONF.GetString("server", "host")
	port, _ := CONF.GetInt("server", "port")
	addr := net.JoinHostPort(host, port)

	// ...
}

Use configuration file

foo-server --config your_config_file_name.conf

Copyright

The TFCloud Go Team. Apache License 2.0