Go Reference · Errors, I/O and Files
Go os.Args and os.Getenv() – arguments and environment
Reference for Go os.Args, os.Getenv, LookupEnv, Setenv, Environ and ExpandEnv: reading command-line arguments and environment variables safely.
os.Args is a slice of strings holding the command line of the running program. os.Args[0] is the program name (or path) and the elements after it are the arguments the user typed. Environment variables are key/value pairs inherited from the parent process; os.Getenv and its relatives read and change them.
Together these two are the simplest way to configure a program. For anything beyond a couple of positional arguments, use the flag package, which parses os.Args for you.
if len(os.Args) < 2 { fmt.Fprintln(os.Stderr, "usage: tool <file>") os.Exit(2) } name := os.Args[1] home := os.Getenv("HOME")
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input · what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input · what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
var Args []string func Getenv(key string) string func LookupEnv(key string) (string, bool) func Setenv(key, value string) error func Unsetenv(key string) error func Clearenv() func Environ() []string func ExpandEnv(s string) string func Expand(s string, mapping func(string) string) string
| Name | Description |
|---|---|
Args |
Program name followed by arguments. Never empty in a normal program, but check len before indexing. |
Getenv(key) |
Value of the variable, or "" if unset or empty. |
LookupEnv(key) |
Value plus a boolean that is false when the variable is not set. |
Setenv, Unsetenv |
Change the current process environment; return an error for invalid keys (empty, or containing =). |
Environ() |
A copy of the environment as "KEY=value" strings. |
ExpandEnv(s) |
Replaces $VAR and ${VAR} in s with environment values. |
Expand(s, f) |
Same, using your own mapping function. |
Usage notes
Getenvhides the difference between unset and empty. UseLookupEnvif that matters, for example for feature flags.- Changes are process-local.
Setenvnever affects the parent shell, only this program and the child processes it starts afterwards. - Do not index blindly.
os.Args[1]panics with "index out of range" when no argument was given. - Everything is a string. Convert with
strconvand decide on a default for bad input. - Windows is case-insensitive for variable names, Unix is case-sensitive.
- Keep secrets out of arguments: other users can see command lines in process listings, while environment variables are harder (but not impossible) to read. Files with
0o600permissions are safer for real secrets. - Use
flagfor options. It handles-name value,--name=value, booleans, defaults and usage text. - Tests: use
t.Setenv("KEY", "value"), which restores the old value automatically. ExpandEnvreplaces unknown variables with an empty string, and there is no escape for a literal dollar sign: in the second example$$5loses its$$and prints just5.os.Executable()returns the real path of the running binary, whichos.Args[0]may not.
Related
ospackage,flagpackage,strconv.Atoi(),os/exec- Lessons: Flags and CLI, os, io and bufio