ProductPromotion
Logo

Go.Lang

made by https://0x3d.site

GitHub - caarlos0/env: A simple, zero-dependencies library to parse environment variables into structs
A simple, zero-dependencies library to parse environment variables into structs - caarlos0/env
Visit Site

GitHub - caarlos0/env: A simple, zero-dependencies library to parse environment variables into structs

GitHub - caarlos0/env: A simple, zero-dependencies library to parse environment variables into structs

Installation
go get github.com/caarlos0/env/v11
Getting started
type config struct {
  Home string `env:"HOME"`
}

// parse
var cfg config
err := env.Parse(&cfg)

// parse with generics
cfg, err := env.ParseAs[config]()

You can see the full documentation and list of examples at pkg.go.dev.


Used and supported by

Usage

Caveats

[!CAUTION]

Unexported fields will be ignored by env. This is by design and will not change.

Functions

  • Parse: parse the current environment into a type
  • ParseAs: parse the current environment into a type using generics
  • ParseWithOptions: parse the current environment into a type with custom options
  • ParseAsithOptions: parse the current environment into a type with custom options and using generics
  • Must: can be used to wrap Parse.* calls to panic on error
  • GetFieldParams: get the env parsed options for a type
  • GetFieldParamsWithOptions: get the env parsed options for a type with custom options

Supported types

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

Complete list:

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

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

You may also add custom parsers for your types.

Tags

The following tags are provided:

  • env: sets the environment variable name and optionally takes the tag options described below
  • envDefault: sets the default value for the field
  • envPrefix: can be used in a field that is a complex type to set a prefix to all environment variables used in it
  • envSeparator: sets the character to be used to separate items in slices and maps (default: ,)
  • envKeyValSeparator: sets the character to be used to separate keys and their values in maps (default: :)

env tag options

Here are all the options available for the env tag:

  • ,expand: expands environment variables, e.g. FOO_${BAR}
  • ,file: instructs that the content of the variable is a path to a file that should be read
  • ,init: initialize nil pointers
  • ,notEmpty: make the field errors if the environment variable is empty
  • ,required: make the field errors if the environment variable is not set
  • ,unset: unset the environment variable after use

Parse Options

There are a few options available in the functions that end with WithOptions:

  • Environment: keys and values to be used instead of os.Environ()
  • TagName: specifies another tag name to use rather than the default env
  • RequiredIfNoDef: set all env fields as required if they do not declare envDefault
  • OnSet: allows to hook into the env parsing and do something when a value is set
  • Prefix: prefix to be used in all environment variables
  • UseFieldNameByDefault: defines whether or not env should use the field name by default if the env key is missing
  • FuncMap: custom parse functions for custom types

Documentation and examples

Examples are live in pkg.go.dev, and also in the example test file.

Current state

env is considered feature-complete.

I do not intent to add any new features unless they really make sense, and are requested by many people.

Eventual bug fixes will keep being merged.

Badges

Release Software License Build status Codecov branch Go docs Powered By: GoReleaser Conventional Commits

Related projects

  • envdoc - generate documentation for environment variables from env tags

Stargazers over time

Stargazers over time

Articles
to learn more about the golang concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Queries
or most google FAQ's about GoLang.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory