Description
config is a package that supports reading configuration into a struct from files, environment variables and command-line arguments. All you need is to declare a structure that will hold your configuration and call `Read` method. The library will pool the values from a config file in any well-known format, from env vars that follows simple naming convention, or from command-line arguments
num30/config alternatives and similar packages
Based on the "Configuration" category.
Alternatively, view config alternatives based on common mentions on social networks and blogs.
-
koanf
Simple, extremely lightweight, extensible, configuration management library for Go. Support for JSON, TOML, YAML, env, command line, file, S3 etc. Alternative to viper. -
konfig
Composable, observable and performant config handling for Go for the distributed processing era -
gookit/config
📝 Go configuration manage(load,get,set,export). support JSON, YAML, TOML, Properties, INI, HCL, ENV and Flags. Multi file load, data override merge, parse ENV var. Go应用配置加载管理,支持多种格式,多文件加载,远程文件加载,支持数据合并,解析环境变量名 -
gcfg
read INI-style configuration files into Go structs; supports user-defined types and subsections -
goConfig
DISCONTINUED. goconfig uses a struct as input and populates the fields of this struct with parameters from command line, environment variables and configuration file. -
joshbetz/config
🛠 A configuration library for Go that parses environment variables, JSON files, and reloads automatically on SIGHUP. -
configuro
An opinionated configuration loading framework for Containerized and Cloud-Native applications. -
hocon
go implementation of lightbend's HOCON configuration library https://github.com/lightbend/config -
configure
Configure is a Go package that gives you easy configuration of your project through redundancy -
Genv
Genv is a library for Go (golang) that makes it easy to read and use environment variables in your projects. It also allows environment variables to be loaded from the .env file. -
swap
Instantiate/configure structs recursively, based on build environment. (YAML, TOML, JSON and env).
InfluxDB - Purpose built for real-time analytics at any scale.
Do you think we are missing an alternative of num30/config or a related project?
Popular Comparisons
README
Declarative configuration for Go :rocket:
Features
- declarative way of defining configuration
- reading configuration from file, environment variables or command line arguments in one lines of code
- validation
Example
config
is a package that supports reading configuration into a struct from files, environment variable and command line arguments.
All you need is to declare a config structure and call Read
method.
type Config struct {
DB Database
Debug Debug
}
type Database struct {
Host string
Password string
DbName string
Username string
Port int
}
func main() {
var config Config
err := config.NewConfReader("myconf").Read(&config)
if err != nil {
panic(err)
}
}
When you want to change, for example, DB Host of your applications you can do any of the following:
- create config
myconf.yaml
file in home directorydb: host: "localhost"
- set environment variable. Like
DB_HOST=localhost
- set command line argument. Like
--db.host=localhost
:information_source: Refer to the example that illustrates how to use ConfReader
.
Execute go run examples/main.go
to run the example.
Install :package:
go get github.com/num30/config
Setting Configuration Values :construction_worker:
ConfReader
merges values from all three sources in the following order:
- File
- Environment variables
- Command line arguments
Setting same key in file will be overridden by environment variable and command line argument has the highest priority. However, you can set one key in file and other in env vars or command line args. Those will be merged.
Config File :memo:
Name
ConfReader
will use config name property to search for a config file with that name.
Location
By default, config reader will search for a config file in home or in current directory.
You can override this behavior by using NewConfReader("myconf").WithSearchDirs("/etc/conf")
of config builder
Referring fields
Field names are converted from camel case starting with lower case letter. For example if it code you refer to value as DB.DbName
then it will be converted to
db:
dbName: "mydb"
Format
Config file type could be any type supported by viper: JSON, TOML, YAML, HCL, INI, envfile and Java Properties files.
Environment Variables :package:
To set a flag via environment variable, make all letters uppercase and replace '.' with '_' in path. For example: app.Server.Port -> APP_SERVER_PORT
Environment variables are prefixed with config name
by default. For example NewConfReader("myconf")
will search for environment variables like MYCONF_DB_HOST
This behavior could be overridden by setting NewConfReader("myconf").WithoutPrefix()
Command Line Arguments :computer:
To set a configuration field via command line argument you need to pass and argument prefixes wiht --
and lowercase field name with path. Like --db.host=localhost
Boolean value could be set by passing only flag name like --verbose
You can override name for a flag by using tag flag:"name"
on a field. For example:
type Config struct {
DebugMode bool `flag:"debug"`
}
You can set the flag by calling myapp --debug
Validations :underage:
You can validate fields of you configuration struct by using validate
tag. For example:
type Config struct {
Host string `validate:"required"`
}
If validation fails ConfReader.Read
will return an error.
Contributing :clap:
We love help! Contribute by forking the repo and opening a pull requests or by creating an issue.
Credits :star:
This package is based Viper Special thanks: