Happy-Ferret / env

Tag-based environment configuration for structs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

env

Tag-based environment configuration for structs.

Godoc Build Status Go Report Card

Installation

$ go get -u github.com/codingconcepts/env

Usage

package main

import (
	"fmt"
	"log"
	"time"

	"github.com/codingconcepts/env"
)

type awsConfig struct {
	Secret            []byte        `env:"SECRET" required:"true"`
	Region            string        `env:"REGION"`
	Port              int           `env:"PORT" required:"true"`
	Peers             []string      `env:"PEERS"`
	ConnectionTimeout time.Duration `env:"TIMEOUT"`
}

func main() {
	config := awsConfig{}
	if err := env.Set(&config); err != nil {
		log.Fatal(err)
	}

	...
}
$ ID=1 SECRET=shh PORT=1234 PEERS=localhost:1235,localhost:1236 TIMEOUT=5s go run main.go

Env currently supports the following data types. If you'd like to have more, please get in touch or feel free to create a pull request:

  • bool and []bool
  • string and []string
  • []byte
  • int, int8, int16, int32, int64 and all slice equivalents
  • uint, uint8, uint16, uint32, uint64 and all slice equivalents
  • float32, float64 and all slice equivalents
  • time.Duration and []time.Duration

Default Values

If a field isn't required, it's also possible to specify a default value:

type config struct {
	Address string `env:"ADDRESS" default:"0.0.0.0"`
}

About

Tag-based environment configuration for structs

License:MIT License


Languages

Language:Go 99.7%Language:Makefile 0.3%