env

package module
v0.0.1-beta Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2022 License: MIT Imports: 7 Imported by: 0

README

A package for managing .env files and environment variables

GitHub tag (latest SemVer pre-release) GitHub go.mod Go version GitHub Workflow Status Go Report Card PkgGoDev

Introduction

  • Read from and write to .env files
  • Get and set environment variables
  • No dependencies outside the standard library

Example

package main

import env "github.com/2pisoftware/gofor-little-env"

func main() {
	// Load an .env file and set the key-value pairs as environment variables.
	if err := env.Load("FILE_PATH"); err != nil {
		panic(err)
	}

	// Write a key-value pair to an .env file and call env.Set on it.
	if err := env.Write("KEY", "VALUE", "FILE_PATH", true); err != nil {
		panic(err)
	}

	// Get an environment variable's value with a default backup value.
	value := env.Get("KEY", "DEFAULT_VALUE")

	// Get an environment variable's value, receiving an error if it is not set or is empty.
	value, err := env.MustGet("KEY")
	if err != nil {
		panic(err)
	}

	// Set an environment variable locally.
	if err := env.Set("KEY", "VALUE"); err != nil {
		panic(err)
	}
}

Testing

Run go test ./... in the root directory.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Get

func Get(key, defaultValue string) string

Get gets an environment variable with a default backup value as the second parameter.

func Load

func Load(fileNames ...string) error

Load loads and sets the environment variables from file using fileNames.

func MustGet

func MustGet(key string) (string, error)

MustGet gets an environment variable and will return an error if the environment variable is not set or is empty.

func Set

func Set(key, value string) error

Set is just a wrapper os.Setenv, this is useful for locally overriding environment variables.

func Write

func Write(key, value, fileName string, setAfterWrite bool) error

Write writes a key-value pair to a file that can be set to an environment variable later on with env.Load(). If 'setAfterWrite' is true env.Set will also be called on the key-value pair.

Types

This section is empty.

Jump to

Keyboard shortcuts

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