dotenv

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2025 License: MIT Imports: 13 Imported by: 0

README

Dotenv

go test golangci-lint

This package provides a simple mechanism for loading environment variables values into typed struct fields.

Installation

go get github.com/tangelo-labs/go-dotenv

Usage

The following example assumes that either variables are set or that a .env file exists in the current working directory, or are already defined in the environment.

package main

import (
	"fmt"
	"os"
	"time"

	"github.com/tangelo-labs/go-dotenv"
)

type config struct {
	Foo  string     `env:"ENV_FOO,required" default:"fooValue"`
	Bar  int        `env:"ENV_BAR,notEmpty"`
	IPs  []string	`env:"ENV_IPS" delimiter:";"`
	When time.Time	`env:"ENV_WHEN" default:"2021-12-24T17:04:05Z07:00" timeLayout:"2006-01-02T15:04:05Z07:00"`
}

func main() {
	var cfg config

	if err := dotenv.LoadAndParse(&cfg); err != nil {
		panic(err)
	}

	fmt.Printf("Foo: %s\n", cfg.Foo)
	fmt.Printf("Bar: %d\n", cfg.Bar)
	fmt.Printf("IPs: %+v\n", cfg.IPs)
	fmt.Printf("When: %s\n", cfg.When)
}

See the dotenv.Parse function for further details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotAPointer        = errors.New("not a pointer")
	ErrTimeLayoutRequired = errors.New("missing timeLayout tag")
	ErrRequiredField      = errors.New("required field")
	ErrEmptyField         = errors.New("empty field")
)

List of injection errors.

Functions

func Load

func Load() error

Load loads the environment.

func LoadAndParse

func LoadAndParse(st interface{}) error

LoadAndParse convenience function which first loads environment variables and then injects them into the given struct.

This simplifies the following use case:

if err := dotenv.Load(); err != nil {
	panic(err)
}

env := Config{}

if err := dotenv.Parse(&env); err != nil {
	panic(err)
}

The above code is equivalent to:

env := Config{}

if err := dotenv.LoadAndParse(); err != nil {
	panic(err)
}

See Parse function for more information.

func MustLoadAndParse added in v0.0.3

func MustLoadAndParse(st interface{})

MustLoadAndParse convenience function which calls LoadAndParse and panics if an error is returned.

func MustParse added in v0.0.3

func MustParse(st interface{})

MustParse convenience function which calls Parse and panics if an error is returned.

func Parse

func Parse(st interface{}) error

Parse injects environment variables into the given struct using tag annotations.

The given argument must be a pointer to a struct where values will be injected.

Struct must tag its fields with `env:"VAR_NAME"` to specify the environment variable value to be injected.

Fields may be marked as required using the `required` env option. If a field is required and no environment variable is found, an error will be returned.

However, if you want to make sure that a field is not empty, you can use the `notEmpty` option. In which case an error will be returned if not value is found.

Time fields:

Optionally, the tag `default` may be used to specify a default value for the field. In the case of `time.Time` fields, the `timeLayout` tag must be used to specify the format of the time string.

String slices:

Optionally, the tag `delimiter` may be used to specify a separator for string slices, by default `,` will be used.

For example:

type Config struct {
	Foo  string		`env:"ENV_FOO,required" default:"fooValue"`
	Bar  int		`env:"ENV_BAR,notEmpty"`
	IPs  []string	`env:"ENV_IPS" delimiter:";"`
	When time.Time	`env:"ENV_WHEN" default:"2021-12-24T17:04:05Z07:00" timeLayout:"2006-01-02T15:04:05Z07:00"`
}

Fields without an `env` tag will not be injected.

func WithOverride

func WithOverride(callback func(), kv ...string)

WithOverride overrides the environment variables with the given ones and restores them after the callback is executed.

Any call to the Parse, LoadAndParse or similar within the callback will be affected by the overridden values.

This function will panic if the number of arguments is not even, or if there is an error setting or unsetting the environment variables.

Typical Usage Example:

dotenv.WithOverride(func() {
   functionThatCallsLoadAndParse()
}, "FOO", "bar")

Types

This section is empty.

Jump to

Keyboard shortcuts

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