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/snippet/gTAprrrEqpU


package main

import (
	"fmt"
	"github.com/golangsugar/envisage"
	"log"
	"os"
)

func main() {
	/* Examples */

	// SetString sets the value of the environment variable named by the key.
	if err := envisage.SetString("DB_CONNECTION_STRING", "databasedriver://user:password@serverhost/db?options"); err != nil {
		log.Fatal(err)
	}

	// SetInt sets the value of the environment variable named by the key.
	if err := envisage.SetInt("WEBSERVICE_PORT", 154); err != nil {
		log.Fatal(err)
	}

	// SetF64 sets the value of the environment variable named by the key.
	if err := envisage.SetF64("PRODUCT_PRICE", 14.1544); err != nil {
		log.Fatal(err)
	}

	// SetBool sets the value of the environment variable named by the key.
	if err := envisage.SetBool("BOOLEAN_VALUE", true); err != nil {
		log.Fatal(err)
	}

	if err := os.Setenv("ARRAY_OF_INTEGERS", "45,8,22,5,4,4,666,4,9"); err != nil {
		log.Fatal(err)
	}

	const (
		key          = "DB_CONNECTION_STRING"
		defaultValue = "-"  // defaultValue returned if the value is not present/set in the environment
		twoWay       = true // twoWay updates the environment with the defaultValue, in case of the environment variable is not present/set
		mandatory    = true // mandatory forces and error in case of the variable is absent in the environment
	)

	// Check Test environment variables according given directives
	if err := envisage.Check(key, defaultValue, twoWay, mandatory); err != nil {
		log.Fatal(err)
	}

	fmt.Println(envisage.IsThere("DB_CONNECTION_STRING"))
	// Print true

	fmt.Println(envisage.String("DB_CONNECTION_STRING", defaultValue))
	// Print "databasedriver://user:password@serverhost/db?options"

	fmt.Println(envisage.Int("WEBSERVICE_PORT", 0))
	// Print 154

	fmt.Println(envisage.I64("WEBSERVICE_PORT", 0))
	// Print 154

	fmt.Println(envisage.Bool("BOOLEAN_VALUE", false))
	// Print true

	// F64 returns the env var value as float64
	fmt.Println(envisage.F64("PRODUCT_PRICE", false, 0))
	// Print 14.1544

	// IntS returns the env var value as []int
	fmt.Println(envisage.IntS("ARRAY_OF_INTEGERS", ",", nil))
	// Print []int{45,8,22,5,4,4,666,4,9}
}
Owner
GOLang Sugar
GOLang OpenSource Packages
GOLang Sugar
Similar Resources

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

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

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

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

A mapper of ENVironment variables to Structure for Go

envs a mapper of ENVironment variables to a Structure for Go. This library maps the environment variables to the struct according to the fields' types

Dec 3, 2021

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

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

✨Clean and minimalistic environment configuration reader for Golang

Clean Env Minimalistic configuration reader Overview This is a simple configuration reading tool. It just does the following: reads and parses configu

Jan 8, 2023
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
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
Wg-configurator - This project makes it easier to configure a fleet of servers interconnected via the WireGuard point-to-point virtual private network.

WireGuard Configurator This project makes it easier to configure a fleet of servers interconnected via the WireGuard point-to-point virtual private ne

Mar 29, 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
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
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
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
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
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