Skip to content

Golang library for reading properties from configuration files in JSON and YAML format or from environment variables.

License

Notifications You must be signed in to change notification settings

iglin/go-config

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-config

codecov

Golang library for reading properties from configuration files in JSON and YAML format or from environment variables.

Usage

Create config instance and read properties from it. Supported file formats are JSON and YAML.

If property is missing in config file, library will try to look up it in envirnment variables: in this case property name will be fomatted to upper case and all dots will be replaced with _, e.g. property 'my.test.property1' will be translated to MY_TEST_PROPERTY1 envirnoment variable name.

import goconfig "github.com/iglin/go-config"

func main() {
	config := goconfig.NewConfig("./test_config.yaml", goconfig.Yaml)

	// reading strings
	
	strVal := config.GetString("root.family1.key1")
	strValOrDefault := config.GetString("root.family1.key1", "my-default-val")
	// panics if both property and env variable ROOT_FAMILY1_KEY1 are missing
	requiredStrVal := config.RequireString("root.family1.key1")
	
	// reading ints
	
	intVal := config.GetInt("root.family1.key1")
	intValOrDefault := config.GetInt("root.family1.key1", 1)
	// panics if both property and env variable ROOT_FAMILY1_KEY1 are missing
	requiredIntVal := config.RequireInt("root.family1.key1")
	
	// reading floats
	
	floatVal := config.GetFloat64("root.family1.key1")
	floatValOrDefault := config.GetFloat64("root.family1.key1", 1.1)
	// panics if both property and env variable ROOT_FAMILY1_KEY1 are missing
	requiredFloatVal := config.RequireFloat32("root.family1.key1")
	
	// reading bools
	
	boolVal := config.GetBool("root.family1.key1")
	boolValOrDefault := config.GetBool("root.family1.key1", true)
	// panics if both property and env variable ROOT_FAMILY1_KEY1 are missing
	requiredBoolVal := config.RequireBool("root.family1.key1")
	
	// reading secrets (decodes base64 value before returning the result)
	
	secretStringVal := config.GetSecret("root.family1.key1")
	secretStringValOrDefault := config.GetSecret("root.family1.key1", "default-val")
	// panics if both property and env variable ROOT_FAMILY1_KEY1 are missing
	requiredSecretVal := config.RequireSecret("root.family1.key1")
}

For more examples see test config file test_config.yaml and ./config_test.go

About

Golang library for reading properties from configuration files in JSON and YAML format or from environment variables.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages