Skip to content
/ env Public

About A simple and zero-dependencies library to parse environment variables into structs

License

Notifications You must be signed in to change notification settings

go-zoox/env

Repository files navigation

Env - env to struct


A simple and zero-dependencies library to parse environment variables into structs

PkgGoDev Build Status Go Report Card Coverage Status GitHub issues Release

Load application environment variables from a .env file into the current process.

package main

import (
	"fmt"
	"time"

	"github.com/go-zoox/env"
)

type config struct {
	Home         string         `env:"HOME"`
	Port         int            `env:"PORT,default=8080"`
	Password     string         `env:"PASSWORD,unset"`
	IsProduction bool           `env:"PRODUCTION"`
	Hosts        []string       `env:"HOSTS,separator=;"`
	TempFolder   string         `env:"TEMP_FOLDER,default=/tmp"`
  Duration     time.Duration  `env:"DURATION"`
	StringInts   map[string]int `env:"MAP_STRING_INT"`
}

func main() {
	cfg := config{}
	if err := env.Parse(&cfg); err != nil {
		fmt.Printf("%+v\n", err)
	}

  // or you can use generics
  cfg, err := env.ParseAs[config]()
  if err != nil {
		fmt.Printf("%+v\n", err)
  }

	fmt.Printf("%+v\n", cfg)
}

Inspired by

About

About A simple and zero-dependencies library to parse environment variables into structs

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages