Search⌘ K
AI Features

Understanding Program Configuration

Explore how to configure Go command-line programs effectively by using command-line flags, environment variables, and configuration files. Understand the roles and trade-offs of each method to enable flexible and user-friendly program behavior without modifying code.

The basic idea of configuration is that the user should be able to choose how the program behaves without having to modify the code. We will explore the many facets of configuration such as command-line flags, environment variables, configuration and files.

Code, input, configuration and configuration as code

Most non-trivial programs have input, which they process. The input can vary, but a program typically processes the input in the same way. Discounting programs that by design introduce random elements, running the same code on the same input should produce the same output. Many programs can also be configured to operate in different ways, so the same input will produce different outputs depending on the configuration. For example, the echo command prints a newline by default. If you execute two consecutive echo commands their output will be displayed on two separate lines. Try it:

Shell
echo 'Hi, '; echo there

However, we can configure echo to omit the newline by specifying the -n command-line flag, and as a result the output from the two echo commands will be displayed on the ...