env

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2025 License: AGPL-3.0 Imports: 10 Imported by: 4

README

Environment Module

Tests Status Go Reference Go Report Card

A Go module for managing environment variables with support for .env loading, value processing (base64/obfuscated), and optional vault-based loading.

Features

  • Load environment variables from .env files (Load)
  • Process values with base64: and obfuscated: prefixes automatically
  • Simple and intuitive API for string, bool, int, and float64 types.
  • Each data type (String, Bool, Int, Float) provides four functions for flexible error handling:
    • Get...: Returns the value or a zero-value ("", false, 0) if not found.
    • Get...OrDefault: Returns a specified default value if not found.
    • Get...OrError: Returns an error if not found or invalid.
    • Get...OrPanic: Panics if not found or invalid.
  • Optional encrypted vault loading (LoadVault)
  • Note: Float64-named functions remain available as aliases of Float for compatibility.

API Reference

Loading Functions
  • Load(envFilePath ...string) – Load environment variables from .env files. Defaults to .env and also attempts any additional paths provided.
  • LoadVault(options struct{ Password string; VaultFilePath string; VaultContent string }) error – Load environment variables from an encrypted vault file or a vault string.
String Functions
  • GetString(key string) string
  • GetStringOrDefault(key string, defaultValue string) string
  • GetStringOrError(key string) (string, error)
  • GetStringOrPanic(key string) string
Bool Functions
  • GetBool(key string) bool
  • GetBoolOrDefault(key string, defaultValue bool) bool
  • GetBoolOrError(key string) (bool, error)
  • GetBoolOrPanic(key string) bool
Int Functions
  • GetInt(key string) int
  • GetIntOrDefault(key string, defaultValue int) int
  • GetIntOrError(key string) (int, error)
  • GetIntOrPanic(key string) int
Float Functions
  • GetFloat(key string) float64
  • GetFloatOrDefault(key string, defaultValue float64) float64
  • GetFloatOrError(key string) (float64, error)
  • GetFloatOrPanic(key string) float64

Compatibility: GetFloat64, GetFloat64OrDefault, GetFloat64OrError, and GetFloat64OrPanic are available as aliases.

Installation

go get github.com/dracory/env

Usage

Working with .env Files

Create a .env file in your project root:

DB_HOST="localhost"
DB_PORT="5432"
SECRET_KEY="your-secret-key"
API_TIMEOUT="30"
DEBUG_MODE="true"
Basic Usage
package main

import (
	"fmt"
	"github.com/dracory/env"
	"log"
)

func main() {
	// Load environment variables from .env files
	env.Load(".env")

	// Get a string variable with a default value
	dbHost := env.GetStringOrDefault("DB_HOST", "localhost")

	// Get a required string variable (panics if not set)
	secretKey := env.GetStringOrPanic("SECRET_KEY")

	// Get an integer variable, handle error if not found or invalid
	dbPort, err := env.GetIntOrError("DB_PORT")
	if err != nil {
		log.Fatalf("Invalid or missing DB_PORT: %v", err)
	}

	// Get a boolean variable
	debugMode := env.GetBool("DEBUG_MODE")

	fmt.Printf("Database: %s:%d\n", dbHost, dbPort)
	fmt.Printf("Secret Key Loaded: %v\n", secretKey != "")
	fmt.Printf("Debug Mode: %v\n", debugMode)
}
Value Processing

GetString, GetStringOrDefault, GetStringOrError, and GetStringOrPanic automatically process these prefixes:

  • base64:<encoded> – Decodes using URL-safe base64.
  • obfuscated:<text> – Deobfuscates using github.com/dracory/envenc.

This lets you safely store encoded/obfuscated values in .env or other sources while retrieving plain values at runtime.

Boolean Parsing

Boolean functions (GetBool, etc.) parse values with flexibility:

  • True values: "true", "True", "TRUE", "T", "t", "1", "yes", "Yes", "YES", and any positive number.
  • False values: "false", "False", "FALSE", "F", "f", "0", "no", "No", "NO", and any negative number.
  • Any other or empty value returns false (for GetBool) or the specified default.
Advanced: Env Vault Loading

Env vault loading allows you to load environment variables from an encrypted vault file or a string.

// Load environment variables from an encrypted vault file
err := env.LoadVault(struct {
    Password      string
    VaultFilePath string
    VaultContent  string
}{
    Password:      "your-password",
    VaultFilePath: ".env.vault",
})
if err != nil {
    // handle error
}
Notes on Load

Load() will attempt to load from a default .env file, and then from any additional file paths you pass in. Missing files are silently skipped.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is dual-licensed under the following terms:

  • For non-commercial use, you may choose either the GNU Affero General Public License v3.0 (AGPLv3) or a separate commercial license (see below). You can find a copy of the AGPLv3 at: https://www.gnu.org/licenses/agpl-3.0.txt

  • For commercial use, a separate commercial license is required. Commercial licenses are available for various use cases. Please contact me via my contact page at https://lesichkov.co.uk/contact to obtain a commercial license.

Documentation

Index

Constants

View Source
const (
	// True values
	// - "true", "True", "TRUE"
	// - "T", "t"
	// - "1" (as well as any other positive number)
	// - 0.01 (as well as any other positive float)
	// - "yes", "Yes", "YES"
	// - "on", "On", "ON"
	TrueValues = "true,True,TRUE,T,t,1,yes,Yes,YES,on,On,ON"

	// False values
	// - "false", "False", "FALSE"
	// - "F", "f"
	// - "0", (as well as any other negative number)
	// - 0.00 (as well as any other negative float)
	// - "off", "Off", "OFF"
	FalseValues = "false,False,FALSE,F,f,0,no,No,NO,off,Off,OFF"
)

Variables

This section is empty.

Functions

func GetBool added in v0.4.0

func GetBool(key string) bool

GetBool retrieves the boolean value of an environment variable. It returns false if the key is not found or the value is not a valid boolean.

func GetBoolOrDefault added in v0.4.0

func GetBoolOrDefault(key string, defaultValue bool) bool

GetBoolOrDefault retrieves the boolean value of an environment variable with a default.

func GetBoolOrError added in v0.4.0

func GetBoolOrError(key string) (bool, error)

GetBoolOrError retrieves the boolean value of an environment variable, returning an error if the key is not found or the value is not a valid boolean.

func GetBoolOrPanic added in v0.4.0

func GetBoolOrPanic(key string) bool

GetBoolOrPanic retrieves the boolean value of an environment variable, panicking if not set or on parsing error.

func GetFloat added in v0.4.0

func GetFloat(key string) float64

GetFloat retrieves the float64 value of an environment variable. It returns 0.0 if the key is not found or the value is not a valid float.

func GetFloat64 added in v0.4.0

func GetFloat64(key string) float64

GetFloat64 retrieves the float64 value of an environment variable. It returns 0.0 if the key is not found or the value is not a valid float64.

func GetFloat64OrDefault added in v0.4.0

func GetFloat64OrDefault(key string, defaultValue float64) float64

GetFloat64OrDefault retrieves the float64 value of an environment variable with a default.

func GetFloat64OrError added in v0.4.0

func GetFloat64OrError(key string) (float64, error)

GetFloat64OrError retrieves the float64 value of an environment variable, returning an error if the key is not found or the value is not a valid float64.

func GetFloat64OrPanic added in v0.4.0

func GetFloat64OrPanic(key string) float64

GetFloat64OrPanic retrieves the float64 value of an environment variable, panicking if not set or on parsing error.

func GetFloatOrDefault added in v0.4.0

func GetFloatOrDefault(key string, defaultValue float64) float64

GetFloatOrDefault retrieves the float64 value of an environment variable with a default.

func GetFloatOrError added in v0.4.0

func GetFloatOrError(key string) (float64, error)

GetFloatOrError retrieves the float64 value of an environment variable, returning an error if the key is not found or the value is not a valid float.

func GetFloatOrPanic added in v0.4.0

func GetFloatOrPanic(key string) float64

GetFloatOrPanic retrieves the float64 value of an environment variable, panicking if not set or on parsing error.

func GetInt added in v0.4.0

func GetInt(key string) int

GetInt retrieves the integer value of an environment variable. It returns 0 if the key is not found or the value is not a valid integer.

func GetIntOrDefault added in v0.4.0

func GetIntOrDefault(key string, defaultValue int) int

GetIntOrDefault retrieves the integer value of an environment variable with a default.

func GetIntOrError added in v0.4.0

func GetIntOrError(key string) (int, error)

GetIntOrError retrieves the integer value of an environment variable, returning an error if the key is not found or the value is not a valid integer.

func GetIntOrPanic added in v0.4.0

func GetIntOrPanic(key string) int

GetIntOrPanic retrieves the integer value of an environment variable, panicking if not set or on parsing error.

func GetString added in v0.4.0

func GetString(key string) string

GetString retrieves the string value of an environment variable. It returns an empty string if the key is not found.

func GetStringOrDefault added in v0.4.0

func GetStringOrDefault(key string, defaultValue string) string

GetStringOrDefault retrieves the string value of an environment variable with a default.

func GetStringOrError added in v0.4.0

func GetStringOrError(key string) (string, error)

GetStringOrError retrieves the string value of an environment variable, returning an error if the key is not found.

func GetStringOrPanic added in v0.4.0

func GetStringOrPanic(key string) string

GetStringOrPanic retrieves the string value of an environment variable, panicking if not set.

func Load added in v0.4.0

func Load(envFilePath ...string)

Load loads environment variables from .env files.

If no paths are provided, it will try to load the default .env file.

Parameters:

...envFilePath: The paths to the .env files to load.

Returns:

None.

func LoadVault added in v0.4.0

func LoadVault(options struct {
	Password      string
	VaultFilePath string
	VaultContent  string
}) error

LoadVault loads environment variables from an encrypted vault file or from vault content using the provided password.

Parameters:

Password: The password to use for decrypting the vault file or vault content.
VaultFilePath: The path to the vault file to load.
VaultContent: The content of the vault to load.

Returns:

An error if loading fails.

Types

This section is empty.

Jump to

Keyboard shortcuts

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