flag package

Go's standard library package for parsing command-line options into typed values with defaults and a generated usage message.

In more detail

Declare each option, such as tries := flag.Int("tries", 3, "attempts before giving up"), then call flag.Parse() and read the values through the returned pointers. Bad input prints usage and exits with code 2. It accepts -name or --name, but not bundled single-letter options.

Example

tries := flag.Int("tries", 3, "attempts before giving up")
flag.Parse()
fmt.Println(*tries)

Related terms

Taught in

References