Code Monkey home page Code Monkey logo

env's Introduction

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"`
}

env's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.