Reading and setting environment variables
Environment variables are another way to pass state into an application beyond reading data in from a file or passing it explicitly over the command line. This recipe will explore some very basic getting and setting of environment variables and then work with the highly useful third-party library https://github.com/kelseyhightower/envconfig.
We'll build an application that can read a config via JSON or through environment variables. The next recipe will further explore alternative formats, including TOML and YAML.
Getting ready
Configure your environment according to these steps:
- Refer to the Getting ready section's steps in the Using command-line flags recipe.
- Run the
go get github.com/kelseyhightower/envconfig/
command. - Run the
go get github.com/pkg/errors/
command.
How to do it...
These steps cover writing and running your application:
- From your terminal/console application, create a new directory called
chapter2/envvar
and navigate to that directory.
- Copy...