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

Build Status Go Report Card Coverage Status GoDoc

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 type
  • Int(): Returns data of Int32 type
  • Float(): Returns data of Float64 type
  • Bool(): 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

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

© Şakir Şensoy, 2019 ~ time.Now()

Released under the MIT License

Similar Resources

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 in

Aug 22, 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

🛠 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

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 for setting values to structs' fields from env, flags, files or default tag

Configuration is a library for injecting values recursively into structs - a convenient way of setting up a configuration object. Available features:

Dec 7, 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

Go helpers to manage environment variables

Envh This library is made up of two parts : Env object : it wraps your environments variables in an object and provides convenient helpers. Env tree o

Sep 26, 2022

Environment variables substitution for Go

envsubst Environment variables substitution for Go. see docs below Installation: From binaries Latest stable envsubst prebuilt binaries for 64-bit Lin

Jan 1, 2023
formicidate is a small tool for Go application can update the value of environment variables in a .env file with code

formicidae Update .env files in Go with code. What is fomicidae? formicidate is a small tool for Go application. You can update the value of environme

Jan 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
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
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
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
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
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
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