Parse environment variables to structs in Go

In Go, it’s dead simple to get the value from an environment variable:


But, sometimes you have default values… so you would have to do something like this:

home := os.Getenv("HOME")
if home == "" {

If you need those values in a lot of places, you would end up creating a function for each one or something like that.

I found this to be extremely boring.

So, I created a small lib that let me do this:

package main

import (


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

func main() {
    cfg := Config{}

The lib, of course, is OpenSource.

  • You can grab the source here;
  • Docs are available here;
  • I also posted this in reddit.

That’s all for today!

See ya.

Your ad here.

Related Posts

Creating debs and rpms with Go

A Repository Graveyard

Charting Repository Stars