os.Args
The slice of strings holding the words a Go program was started with, where os.Args[0] is the program itself.
In more detail
os.Args[1:] gives the real arguments. Check len(os.Args) before indexing, or a missing argument panics. For anything beyond one or two arguments, use the flag package, whose flag.Args() returns what is left after options.
Example
args := os.Args[1:]Related terms
- flag package — Go's standard library package for parsing command-line options into typed values with defaults and a generated usage message.
- positional argument — A command-line argument identified by its position rather than by a flag name, such as the host names after the options.
- len — The built-in Go function that returns how many items a string, slice, map, array or channel holds.
- slice expression — Writing s[low:high] to get a new slice that views part of an existing slice, array or string.
Taught in
- Files and the os Package — Concurrent Systems Programming with Go
- Parsing Arguments with flag — Concurrent Systems Programming with Go