Repolode
caarlos0

caarlos0/env

A zero-dependency Go library that parses environment variables into typed structs using struct tags.

Stars
6,356
Forks
299
Open issues
44
Last commit
2 days ago

What is it?

env is a Go library for loading configuration from environment variables. You define a struct, annotate its fields with `env` tags naming the variables to read, and call Parse or ParseAs to fill the struct with typed values. It has no external dependencies.

Beyond basic string and number fields, env supports durations, URLs, time zones, slices and maps with configurable separators, nested structs with prefixes, default values, and custom parsers for your own types. Tag options cover required and non-empty checks, reading values from files, variable expansion, and unsetting variables after use.

The library is the 'feature-complete' v11 release line, with generics-based helpers and options for changing tag names, providing a custom environment source, and hooking into value assignment.

Key features

  • 01Parses environment variables into structs using `env` struct tags.
  • 02Supports built-in numeric, string, bool, duration, URL and time zone types.
  • 03Handles slices, maps, pointers and nested structs with prefixes.
  • 04Offers required, notEmpty, file-reading and variable expansion options.
  • 05Allows custom parsers for user-defined types.

Use cases

  • Loading application configuration from environment variables in a Go service.
  • Validating that required settings are present at startup.
  • Reading container or 12-factor style configuration into typed Go structs.

Quick start

  1. 1

    Install the library with go get

    go get github.com/caarlos0/env/v11
  2. 2

    Define a struct with env tags and call env.Parse to load environment variables into it

Things to know

  • Unexported struct fields are ignored by design.

Topics

FAQ

What is env?
A zero-dependency Go library that parses environment variables into typed structs using struct tags. env is a Go library for loading configuration from environment variables. You define a struct, annotate its fields with `env` tags naming the variables to read, and call Parse or ParseAs to fill the struct with typed values. It has no external dependencies.
Is env free to use? What license does it have?
env is open source under the MIT license. Check the license terms before using it in commercial products.
Is env still maintained?
Actively maintained. The last commit to env was on Nov 5, 2026.
What can I use env for?
Loading application configuration from environment variables in a Go service. Validating that required settings are present at startup. Reading container or 12-factor style configuration into typed Go structs.
How do I install env?
Install the library with go get: go get github.com/caarlos0/env/v11 Define a struct with env tags and call env.Parse to load environment variables into it
Where can I get env?
The source code and installation instructions are on GitHub: https://github.com/caarlos0/env

Summary generated by AI from the project's README. GitHub stats refresh daily.

Related projects