gloader

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2025 License: MIT Imports: 6 Imported by: 0

README

Go logo gloader (v.1.0.0)

A lightweight, flexible environment variable loader for Go.

Supports loading from .env files or any io.Reader source (such as strings, network streams, or in-memory data).

Go


Features

  • Load env vars from one or more io.Readers
  • Automatically falls back to .env file in none are provided
  • Supports panicking or fallback lookups
  • Simple API for flexible integration
  • No external dependencies

Installation

go get github.com/RecursionExcursion/gloader

Usage

Load from default .env file
loader := &gloader.EnvLoader{}
_ = loader.LoadEnv()

foo := loader.MustGet("FOO")
Load from custom readers (in-memory)
r1 := strings.NewReader("FOO=bar")
r2 := strings.NewReader("BAR=baz")

loader := &gloader.EnvLoader{}
_ = loader.LoadEnv(r1, r2)

fmt.Println(os.Getenv("FOO")) // bar

Get API

loader.Get("FOO")                  // string, error
loader.MustGet("FOO")             // string, panics if missing
loader.GetOrFallback("FOO", "x")  // string, returns fallback
loader.GetOrDefault("FOO")        // string, returns ""

.env Format

Each line should follow:

KEY=VALUE

Lines starting with # are ignored. Whitespace is trimmed.


License

MIT License © 2025 RecursionExcursion(https://github.com/RecursionExcursion)

See LICENSE for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EnvLoader

type EnvLoader struct {
	// contains filtered or unexported fields
}

func (*EnvLoader) Get

func (el *EnvLoader) Get(key string) (string, error)

returns value associated with key or an error if no key is found

func (*EnvLoader) GetOrDefault

func (el *EnvLoader) GetOrDefault(key string) string

returns value associated with key or zero value ("") if no key is found

func (*EnvLoader) GetOrFallback

func (el *EnvLoader) GetOrFallback(key string, fallback string) string

returns value associated with key or fallback if no key is found

func (*EnvLoader) LoadEnv

func (el *EnvLoader) LoadEnv(readers ...io.Reader) error

LoadEnv loads environment variables from one or more files into the process's environment. Files should contain lines in the format KEY=VALUE. If no files are provided, it defaults to loading from ".env".

func (*EnvLoader) MustGet

func (el *EnvLoader) MustGet(key string) string

returns value associated with key or panics if no key is found

Jump to

Keyboard shortcuts

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