Skip to content

Latest commit

 

History

History
61 lines (45 loc) · 1.55 KB

README.md

File metadata and controls

61 lines (45 loc) · 1.55 KB

envconfig

Go Reference Go Report Card Test License

A low dependency package for .env files.

Install

go get github.com/h-dav/envconfig

Usage

Create your config structure:

type Config struct {
    LogLevel string `env:"LOG_LEVEL" default:"info"`
    Server struct {
        Port string `env:"PORT" required:"true"`
    } `prefix:"SERVER_"`
}

Then just read pass your config structure along with the path to your .env file to envconfig.Set:

var cfg Config
err := envconfig.Set("./config/default.env", &cfg)

The corresponding .env file for this example:

LOG_LEVEL: debug
SERVER_PORT: 8080

Options

  • required: true or false
  • default: Fall back value if environment variable is not set.
  • prefix: Used for nested structures.
  • Text Replacement: ${EXAMPLE} can be used to insert other environment variables.

Supported data types

  • string
  • int
  • float
  • bool
  • []string
  • []int
  • []float

Note

This package takes heavy inspiration from httputil for handling reflection.