Giter Club home page Giter Club logo

env's Introduction

env Build Status Coverage Status Stories in Ready

A KISS way to deal with environment variables in Go.

Why

At first, it was boring for me to write down an entire function just to get some var from the environment and default to another in case it's missing.

For that manner, I wrote a GetOr function in the go-idioms project.

Then, I got pissed about writing os.Getenv, os.Setenv, os.Unsetenv... it kind of make more sense to me write it as env.Get, env.Set, env.Unset. So I did.

Then I got a better idea: to use struct tags to do all that work for me.

Example

A very basic example (check the examples folder):

package main

import (
	"fmt"
	"os"

	"github.com/caarlos0/env"
)

type config struct {
	Home         string `env:"HOME"`
	Port         int    `env:"PORT" envDefault:"3000"`
	IsProduction bool   `env:"PRODUCTION"`
}

func main() {
	os.Setenv("HOME", "/tmp/fakehome")
	cfg := config{}
	env.Parse(&cfg)
	fmt.Println(cfg)
}

You can run it like this:

$ PRODUCTION=true go run examples/first.go
{/tmp/fakehome 3000 true}

Supported types and defaults

Currently we only support string, bool and int.

If you set the envDefault tag for something, this value will be used in the case of absence of it in the environment. If you don't do that AND the environment variable is also not set, the zero-value of the type will be used: empty for strings, false for bools and 0 for ints.

env's People

Contributors

caarlos0 avatar mantyr avatar

Watchers

 avatar  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.