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

go-config

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.GetBool("root.family1.key1", "default-val")
	// panics if both property and env variable ROOT_FAMILY1_KEY1 are missing
	requiredSecretVal := config.RequireBool("root.family1.key1")
}

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

Similar Resources

Light weight, extensible configuration management library for Go. Built in support for JSON, TOML, YAML, env, command line, file, S3 etc. Alternative to viper.

Light weight, extensible configuration management library for Go. Built in support for JSON, TOML, YAML, env, command line, file, S3 etc. Alternative to viper.

koanf (pronounced conf; a play on the Japanese Koan) is a library for reading configuration from different sources in different formats in Go applicat

Jan 8, 2023

Go-config - Config parser for go that supports environment vars and multiple yaml files

go-multiconfig This package is able to parse yaml config files. It supports gett

Jun 23, 2022

A Go port of Ruby's dotenv library (Loads environment variables from `.env`.)

GoDotEnv A Go (golang) port of the Ruby dotenv project (which loads env vars from a .env file) From the original Library: Storing configuration in the

Jan 5, 2023

A Go library for parsing struct tags from environment variables.

Envconfig Envconfig populates struct field values based on environment variables or arbitrary lookup functions. It supports pre-setting mutations, whi

Jan 2, 2023

Library providing routines to merge and validate JSON, YAML and/or TOML files

Library providing routines to merge and validate JSON, YAML and/or TOML files

CONFLATE Library providing routines to merge and validate JSON, YAML, TOML files and/or structs (godoc) Typical use case: Make your application config

Sep 26, 2022

JSON or YAML configuration wrapper with convenient access methods.

Config Package config provides convenient access methods to configuration stored as JSON or YAML. This is a fork of the original version. This version

Dec 16, 2022

Lightweight package that makes easier and safer to deal with environment variables.

Envisage A lightweight package that makes easier and safer to deal with environment variables. Example Try it on On GoPlay https://goplay.tools/snippe

Apr 11, 2022

Simple lib to parse environment variables to structs

env Simple lib to parse envs to structs in Go. Example A very basic example: package main import ( "fmt" "time" // if using go modules "github.c

Jan 9, 2023

Un-marshaling environment variables to Go structs

envcfg Un-marshaling environment variables to Go structs Getting Started Let's set a bunch of environment variables and then run your go app #!/usr/bi

Sep 26, 2022
🛠 A configuration library for Go that parses environment variables, JSON files, and reloads automatically on SIGHUP
🛠 A configuration library for Go that parses environment variables, JSON files, and reloads automatically on SIGHUP

config A small configuration library for Go that parses environment variables, JSON files, and reloads automatically on SIGHUP. Example func main() {

Dec 11, 2022
Golang Configuration tool that support YAML, JSON, TOML, Shell Environment

Configor Golang Configuration tool that support YAML, JSON, TOML, Shell Environment (Supports Go 1.10+) Usage package main import ( "fmt" "github.c

Dec 29, 2022
Golang library for managing configuration data from environment variables

envconfig import "github.com/kelseyhightower/envconfig" Documentation See godoc Usage Set some environment variables: export MYAPP_DEBUG=false export

Dec 26, 2022
Tmpl - A tool to apply variables from cli, env, JSON/TOML/YAML files to templates

tmpl allows to apply variables from JSON/TOML/YAML files, environment variables or CLI arguments to template files using Golang text/template and functions from the Sprig project.

Nov 14, 2022
Small library to read your configuration from environment variables

envconfig envconfig is a library which allows you to parse your configuration from environment variables and fill an arbitrary struct. See the example

Nov 3, 2022
goconfig uses a struct as input and populates the fields of this struct with parameters from command line, environment variables and configuration file.

goconfig goconfig uses a struct as input and populates the fields of this struct with parameters from command line, environment variables and configur

Dec 15, 2022
Environment variables configuration package for Go microservices.

gocfg Environment variables configuration package for Go microservices. It helps validate environment variable values and set default values if needed

Dec 30, 2021
Tinyini - Bare-bones Go library for reading INI-like configuration files

tinyini tinyini is a minimalistic library for parsing INI-like configuration files. example configuration file globalkey = globalvalue [section] key

Jan 10, 2022
Read files into environment variables and execute command

read-file-to-env -- Read files into environment variables and execute command Example use: read-file-to-env -one-line=HOST=/etc/hostname sh -c 'echo h

Nov 12, 2021
Quickly read variables from environment files

go-quick-env Quickly read variables from environment files The best way to import environment variables to your code, is by using .env files. This lib

May 11, 2021