env

package module
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2022 License: GPL-3.0 Imports: 5 Imported by: 0

README

Environment variable loader

Why use this over os.Getenv("FOO")? Read below to find out!

This package will load an environment variable file (usually a .env file placed in the root) and have these variables globally available using env.Get("FOO"). You can optionally allow 'real' environment variables to override the variables in the .env file allowing for some nice defaults to be set in the case of a missing environment variable.

What's more, unlike other env loaders, the variables contained in your .env file do not actually get set in the real environment and are only available to the running Go application making them more secure to malicious code that may be running on the same server.

Using env.Get("XXX") can completly replace os.Getenv("XXX") if there is no variable in the file then next check will be the 'real' environment.

Usage

go get github.com/SamuelBanksTech/Go-Environment

Basic Example
package main

import (
	"fmt"
    "log"
	"github.com/SamuelBanksTech/Go-Environment"
)

func main() {

	//Init env vars
	envinit := env.Environment{
		EnvPath:             ".env",
		EnableOsEnvOverride: true,
		HideOutput:          false,
	}

	err := envinit.LoadEnv()
	if err != nil {
		log.Fatalln(err)
	}
	
	fmt.Println(env.Get("FOO"))
	
}
Example .env
FOO=bar
BAR=donk
#IGNOREDVAR=foobar

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(key string) string

Get simply returns the matching key value or falling back to os env

Types

type Environment

type Environment struct {
	// EnvPath is the path to your environment variable file, usually .env in the root but can be whatever you like
	EnvPath string
	// EnableOsEnvOverride allows any matching os level environment variables to override any contained in the environment file
	EnableOsEnvOverride bool
	// HideOutput simply shows or hides console output of parsed environment variables
	HideOutput bool
}

Environment struct

func (*Environment) LoadEnv

func (e *Environment) LoadEnv() error

LoadEnv loads the specified environment file and splits them into key value pairs these are then stored in a globally accessible map via a getter method if Environment.EnableOsOverride is set to true any matching keys that exist in the actual environment will override the value in the file

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL