env

A KISS way to deal with environment variables in Go.

env

Build Status
Coverage Status

Simple lib to parse envs to structs in Go.

Example

A very basic example:

package main

import (
	"fmt"
	"time"

  // if using go modules
  "github.com/caarlos0/env/v6"

  // if using dep/others
  "github.com/caarlos0/env"
)

type config struct {
	Home         string        `env:"HOME"`
	Port         int           `env:"PORT" envDefault:"3000"`
	IsProduction bool          `env:"PRODUCTION"`
	Hosts        []string      `env:"HOSTS" envSeparator:":"`
	Duration     time.Duration `env:"DURATION"`
	TempFolder   string        `env:"TEMP_FOLDER" envDefault:"${HOME}/tmp" envExpand:"true"`
}

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

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

You can run it like this:

$ PRODUCTION=true HOSTS="host1:host2:host3" DURATION=1s go run main.go
{Home:/your/home Port:3000 IsProduction:true Hosts:[host1 host2 host3] Duration:1s}

Supported types and defaults

Out of the box all built-in types are supported, plus a few others that
are commonly used.

Complete list:

  • string
  • bool
  • int
  • int8
  • int16
  • int32
  • int64
  • uint
  • uint8
  • uint16
  • uint32
  • uint64
  • float32
  • float64
  • string
  • time.Duration
  • encoding.TextUnmarshaler
  • url.URL

Pointers, slices and slices of pointers of those types are also supported.

You can also use/define a custom parser func for any
other type you want.

If you set the envDefault tag for something, this value will be used in the
case of absence of it in the environment.

By default, slice types will split the environment value on ,; you can change
this behavior by setting the envSeparator tag.

If you set the envExpand tag, environment variables (either in ${var} or
$var format) in the string will be replaced according with the actual value
of the variable.

Unexported fields are ignored.

Custom Parser Funcs

If you have a type that is not supported out of the box by the lib, you are able
to use (or define) and pass custom parsers (and their associated reflect.Type)
to the env.ParseWithFuncs() function.

In addition to accepting a struct pointer (same as Parse()), this function
also accepts a map[reflect.Type]env.ParserFunc.

env also ships with some pre-built custom parser funcs for common types. You
can check them out here.

If you add a custom parser for, say Foo, it will also be used to parse
*Foo and []Foo types.

This directory contains pre-built, custom parsers that can be used with env.ParseWithFuncs
to facilitate the parsing of envs that are not basic types.

Check the example in the go doc
for more info.

Required fields

The env tag option required (e.g., env:"tagKey,required") can be added
to ensure that some environment variable is set. In the example above,
an error is returned if the config struct is changed to:

type config struct {
    Home         string   `env:"HOME"`
    Port         int      `env:"PORT" envDefault:"3000"`
    IsProduction bool     `env:"PRODUCTION"`
    Hosts        []string `env:"HOSTS" envSeparator:":"`
    SecretKey    string   `env:"SECRET_KEY,required"`
}

Stargazers over time

Stargazers over time

主要指標

概覽
名稱與所有者xtaci/kcp-go
主編程語言Go
編程語言Go (語言數: 2)
平台
許可證MIT License
所有者活动
創建於2015-06-16 06:15:55
推送於2025-06-10 06:05:23
最后一次提交2025-06-10 13:59:46
發布數149
最新版本名稱v5.6.21 (發布於 )
第一版名稱v1.0.0 (發布於 )
用户参与
星數4.3k
關注者數143
派生數757
提交數805
已啟用問題?
問題數209
打開的問題數54
拉請求數38
打開的拉請求數10
關閉的拉請求數32
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?