base on A simple, zero-dependencies library to parse environment variables into structs <p align="center"> <img alt="GoReleaser Logo" src="https://becker.software/env.png" height="140" /> <p align="center">A simple, zero-dependencies library to parse environment variables into structs.</p> </p> ###### Installation ```bash go get github.com/caarlos0/env/v11 ``` ###### Getting started ```go 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](https://pkg.go.dev/github.com/caarlos0/env/v11). --- ## Used and supported by <p> <a href="https://encore.dev"> <img src="https://user-images.githubusercontent.com/78424526/214602214-52e0483a-b5fc-4d4c-b03e-0b7b23e012df.svg" width="120px" alt="encore icon" /> </a> <br/> <br/> <b>Encore – the platform for building Go-based cloud backends.</b> <br/> </p> ## 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 - `ParseAsWithOptions`: 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` - `PrefixTagName`: specifies another prefix tag name to use rather than the default `envPrefix` - `DefaultValueTagName`: specifies another default tag name to use rather than the default `envDefault` - `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](https://pkg.go.dev/github.com/caarlos0/env/v11), and also in the [example test file](./example_test.go). ## 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](https://img.shields.io/github/release/caarlos0/env.svg?style=for-the-badge)](https://github.com/goreleaser/goreleaser/releases/latest) [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=for-the-badge)](/LICENSE.md) [![Build status](https://img.shields.io/github/actions/workflow/status/caarlos0/env/build.yml?style=for-the-badge&branch=main)](https://github.com/caarlos0/env/actions?workflow=build) [![Codecov branch](https://img.shields.io/codecov/c/github/caarlos0/env/main.svg?style=for-the-badge)](https://codecov.io/gh/caarlos0/env) [![Go docs](https://img.shields.io/badge/godoc-reference-blue.svg?style=for-the-badge)](http://godoc.org/github.com/caarlos0/env/v11) [![Powered By: GoReleaser](https://img.shields.io/badge/powered%20by-goreleaser-green.svg?style=for-the-badge)](https://github.com/goreleaser) [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg?style=for-the-badge)](https://conventionalcommits.org) ## Related projects - [envdoc](https://github.com/g4s8/envdoc) - generate documentation for environment variables from `env` tags ## Stargazers over time [![Stargazers over time](https://starchart.cc/caarlos0/env.svg)](https://starchart.cc/caarlos0/env) ", Assign "at most 3 tags" to the expected json: {"id":"4726","tags":[]} "only from the tags list I provide: [{"id":77,"name":"3d"},{"id":89,"name":"agent"},{"id":17,"name":"ai"},{"id":54,"name":"algorithm"},{"id":24,"name":"api"},{"id":44,"name":"authentication"},{"id":3,"name":"aws"},{"id":27,"name":"backend"},{"id":60,"name":"benchmark"},{"id":72,"name":"best-practices"},{"id":39,"name":"bitcoin"},{"id":37,"name":"blockchain"},{"id":1,"name":"blog"},{"id":45,"name":"bundler"},{"id":58,"name":"cache"},{"id":21,"name":"chat"},{"id":49,"name":"cicd"},{"id":4,"name":"cli"},{"id":64,"name":"cloud-native"},{"id":48,"name":"cms"},{"id":61,"name":"compiler"},{"id":68,"name":"containerization"},{"id":92,"name":"crm"},{"id":34,"name":"data"},{"id":47,"name":"database"},{"id":8,"name":"declarative-gui "},{"id":9,"name":"deploy-tool"},{"id":53,"name":"desktop-app"},{"id":6,"name":"dev-exp-lib"},{"id":59,"name":"dev-tool"},{"id":13,"name":"ecommerce"},{"id":26,"name":"editor"},{"id":66,"name":"emulator"},{"id":62,"name":"filesystem"},{"id":80,"name":"finance"},{"id":15,"name":"firmware"},{"id":73,"name":"for-fun"},{"id":2,"name":"framework"},{"id":11,"name":"frontend"},{"id":22,"name":"game"},{"id":81,"name":"game-engine "},{"id":23,"name":"graphql"},{"id":84,"name":"gui"},{"id":91,"name":"http"},{"id":5,"name":"http-client"},{"id":51,"name":"iac"},{"id":30,"name":"ide"},{"id":78,"name":"iot"},{"id":40,"name":"json"},{"id":83,"name":"julian"},{"id":38,"name":"k8s"},{"id":31,"name":"language"},{"id":10,"name":"learning-resource"},{"id":33,"name":"lib"},{"id":41,"name":"linter"},{"id":28,"name":"lms"},{"id":16,"name":"logging"},{"id":76,"name":"low-code"},{"id":90,"name":"message-queue"},{"id":42,"name":"mobile-app"},{"id":18,"name":"monitoring"},{"id":36,"name":"networking"},{"id":7,"name":"node-version"},{"id":55,"name":"nosql"},{"id":57,"name":"observability"},{"id":46,"name":"orm"},{"id":52,"name":"os"},{"id":14,"name":"parser"},{"id":74,"name":"react"},{"id":82,"name":"real-time"},{"id":56,"name":"robot"},{"id":65,"name":"runtime"},{"id":32,"name":"sdk"},{"id":71,"name":"search"},{"id":63,"name":"secrets"},{"id":25,"name":"security"},{"id":85,"name":"server"},{"id":86,"name":"serverless"},{"id":70,"name":"storage"},{"id":75,"name":"system-design"},{"id":79,"name":"terminal"},{"id":29,"name":"testing"},{"id":12,"name":"ui"},{"id":50,"name":"ux"},{"id":88,"name":"video"},{"id":20,"name":"web-app"},{"id":35,"name":"web-server"},{"id":43,"name":"webassembly"},{"id":69,"name":"workflow"},{"id":87,"name":"yaml"}]" returns me the "expected json"