Mastering Unix Environment Variables

GoGoBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This challenge will cover the basics of environment variables in Unix programs. Environment variables are used to convey configuration information to programs.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("`Go`")) -.-> go/CommandLineandEnvironmentGroup(["`Command Line and Environment`"]) go/CommandLineandEnvironmentGroup -.-> go/environment_variables("`Environment Variables`") subgraph Lab Skills go/environment_variables -.-> lab-15390{{"`Mastering Unix Environment Variables`"}} end

Environment Variables

In this challenge, you will need to set, get, and list environment variables.

Requirements

  • Use os.Setenv to set a key/value pair.
  • Use os.Getenv to get a value for a key.
  • Use os.Environ to list all key/value pairs in the environment.
  • Use strings.SplitN to split the key and value.

Example

## Running the program shows that we pick up the value
## for `FOO` that we set in the program, but that
## `BAR` is empty.
$ go run environment-variables.go
FOO: 1
BAR:

## The list of keys in the environment will depend on your
## particular machine.
TERM_PROGRAM
PATH
SHELL
...
FOO

## If we set `BAR` in the environment first, the running
## program picks that value up.
$ BAR=2 go run environment-variables.go
FOO: 1
BAR: 2
...

Summary

In this challenge, you learned how to set, get, and list environment variables in Unix programs. This is a fundamental concept that is used in many programs to convey configuration information.

Other Go Tutorials you may like