Documentation ¶
Overview ¶
Package confloader is a simple configuration file loader that accepts both JSON and YAML file formats.
Configuration file (JSON):
{ "paramString": "foo" }
Code:
package main import ( "fmt" cl "github.com/blakelead/confloader" ) func main() { cnf, err := confloader.Load("conf.json") if err != nil { panic(err) } ps := cnf.GetString("paramString") fmt.Println(ps) // foo }
Parameters can be of type string, int, float, bool, duration and array. See the Github Readme for a more thorough example.
Index ¶
- type Config
- func (c *Config) Get(p string) interface{}
- func (c *Config) GetBool(p string) (b bool)
- func (c *Config) GetBoolArray(p string) (a []bool)
- func (c *Config) GetDuration(p string) (d time.Duration)
- func (c *Config) GetDurationArray(p string) []time.Duration
- func (c *Config) GetFloat(p string) (f float64)
- func (c *Config) GetFloatArray(p string) (a []float64)
- func (c *Config) GetInt(p string) int
- func (c *Config) GetIntArray(p string) []int
- func (c *Config) GetString(p string) (s string)
- func (c *Config) GetStringArray(p string) (a []string)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config map[string]interface{}
Config is a map of parameters. Each key corresponds to the absolute path of the parameter in the configuration file. Values are the raw value of the parameter.
func Load ¶
Load loads a configuration file and returns a Config object, or an error if file could not be read or unmarshalled, or if the file doesn't exist.
func (*Config) Get ¶
Get gets value of parameter p. p should be the absolute path to the parameter. Example: { "param1": { "param2": 3.14 } }; to access param2, p should be "param1.param2".
func (*Config) GetBool ¶
GetBool gets number value of parameter p. If parameter is a number, the boolean will be true if parameter is not 0, false otherwise.
func (*Config) GetBoolArray ¶
GetBoolArray gets a bool slice from parameter p.
func (*Config) GetDuration ¶
GetDuration gets duration value of parameter p. p can have suffixes like s, ms, h, etc. In fact the same as standard time.ParseDuration().
func (*Config) GetDurationArray ¶
GetDurationArray gets a duration slice from parameter p.
func (*Config) GetFloat ¶
GetFloat gets float value of parameter p. If parameter is a boolean, the number will be 1.0 if true, 0.0 if false.
func (*Config) GetFloatArray ¶
GetFloatArray gets a float64 slice from parameter p.
func (*Config) GetIntArray ¶
GetIntArray gets a int slice from parameter p.
func (*Config) GetString ¶
GetString gets string value of parameter p. If parameter is a number, the number is converted to a string. If parameter is a boolean, the string will be "true" or "false".
func (*Config) GetStringArray ¶
GetStringArray gets a string slice from parameter p.