Description
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.
Genv alternatives and similar packages
Based on the "Configuration" category.
Alternatively, view Genv alternatives based on common mentions on social networks and blogs.
-
kelseyhightower/envconfig
Go library for managing configuration data from environment variables. -
konfig
Composable, observable and performant config handling for Go for the distributed processing era. -
koanf
Light weight, extensible library for reading config in Go applications. Built in support for JSON, TOML, YAML, env, command line. -
hjson
Human JSON, a configuration file format for humans. Relaxed syntax, fewer mistakes, more comments. -
gookit/config
application config manage(load,get,set). support JSON, YAML, TOML, INI, HCL. multi file load, data override merge. -
gcfg
read INI-style configuration files into Go structs; supports user-defined types and subsections -
goConfig
Parse a struct as input and populates the fields of this struct with parameters fom command line, environment variables and configuration file. -
joshbetz/config
A small configuration library for Go that parses environment variables, JSON files, and reloads automatically on SIGHUP. -
harvester
Harvester, a easy to use static and dynamic configuration package supportig seeding, env vars and Consul integration. -
onion
Layer based configuration for Go, Supports JSON, TOML, YAML, properties, etcd, env, and encryption using PGP. -
configuro
opinionated configuration loading & validation framework from ENV and Files focused towards 12-Factor compliant applications. -
configure
Provides configuration through multiple sources, including JSON, flags and environment variables. -
configuration
Library for initializing configuration structs from env variables, files, flags and 'default' tag. -
hocon
Configuration library for working with the HOCON(a human-friendly JSON superset) format, supports features like environment variables, referencing other values, comments and multiple files. -
sprbox
Build-environment aware toolbox factory and agnostic config parser (YAML, TOML, JSON and Environment vars). -
swap
Instantiate/configure structs recursively, based on build environment. (YAML, TOML, JSON and env). -
gonfig
Tag-based configuration parser which loads values from different providers into typesafe struct. -
gone/jconf
Modular JSON configuration. Keep you config structs along the code they configure and delegate parsing to submodules without sacrificing full config serialization.
Get performance insights in less than 4 minutes
Do you think we are missing an alternative of Genv or a related project?
Popular Comparisons
README
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.
Installation
go get github.com/sakirsensoy/genv
Usage
Create a .env
file in the root directory of your project and enter the environment variables you want to use:
# .env
APP_HOST=localhost
APP_PORT=1234
APP_DEBUG=true
In the meantime, it is optional to use the .env
file. You can also send environment variables to your project in classic ways:
APP_HOST=localhost ./myproject
Rather than using your environment variables directly in your project, it is better to map and match them with a struct. Below you can see how we get our application parameters from environment variables:
// config/config.go
package config
import "github.com/sakirsensoy/genv"
type appConfig struct {
Host string
Port int
Debug bool
}
var App = &appConfig{
Host: genv.Key("APP_HOST").String(),
Port: genv.Key("APP_PORT").Default(8080).Int(),
Debug: genv.Key("APP_DEBUG").Default(false).Bool(),
}
In main.go
we first include the package that allows you to automatically load the environment variables from the .env file. Then we can include and use the parameters defined in config.go
:
// main.go
package main
import (
_ "github.com/sakirsensoy/genv/dotenv/autoload"
"fmt"
"myproject/config"
)
func main() {
fmt.Println(config.App.Host) // localhost
fmt.Println(config.App.Port) // 1234
fmt.Println(config.App.Debug) // true
}
Accessing Environment Variables
Genv provides an easy-to-use API for accessing environment variables.
First we specify the key to the variable want to access
var env = genv.Key("MY_VARIABLE")
Define default value (optional)
env = env.Default("default_value")
Finally, we specify the type of the environment variable and pass its contents to another variable
var myVariable = env.String()
Supported Variable Types
Genv provides support for the following data types:
String()
: Returns data of String typeInt()
: Returns data of Int32 typeFloat()
: Returns data of Float64 typeBool()
: Returns data of Bool type
For other types, you can use type conversion:
var stringValue = genv.Key("KEY").String()
var byteArrayValue = []byte(stringValue)
Contributing
Thanks in advance for your contributions :) I would appreciate it if you make sure that the API remains simple when developing.
code changes without tests will not be accepted
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Added some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
License
© Şakir Şensoy, 2019 ~ time.Now()
Released under the MIT License
*Note that all licence references and agreements mentioned in the Genv README section above
are relevant to that project's source code only.