Skip to content

Read from, write to .env files, get and set environment variables.

License

Notifications You must be signed in to change notification settings

2pisoftware/gofor-little-env

 
 

Repository files navigation

A package for managing .env files and environment variables

GitHub tag (latest SemVer pre-release) GitHub go.mod Go version License: MIT 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.

About

Read from, write to .env files, get and set environment variables.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 97.1%
  • Shell 2.9%