Flag package
The flag package is Go’s standard-library package for defining, parsing, retrieving, and documenting command-line flags.
Go in Practice, Second Edition
The Go flag package parses command-line flags and provides APIs for defining flags and generating their help text.
- How it is used
- Defines and parses options for a small Go command-line application.
- How deep it goes
- Works through typed registration, pointer access, parsing, aliases, and help formatting, but leaves richer command interfaces to frameworks.
- Examples
- Uses a bilingual greeting CLI, invalid-flag usage output, and a `VisitAll` help formatter.
- What you can do after
- Apply it via Go's flag package to parse typed options, add explicit aliases, and format help text.
Read how Go in Practice, Second Edition explains flag package →
Learn Go with Pocket-Sized Projects
The flag package is a Go standard-library package for parsing command-line flags and converting their values to supported types.
- How it is used
- Parses named command-line options and separates them from positional arguments in small Go command-line applications.
- How deep it goes
- Explains declaration, defaults, parsing, pointers, and argument retrieval through runnable examples while deferring fuller pointer treatment.
- Examples
- Uses a language-selecting greeting program and a currency converter with source, target, and amount inputs.
- What you can do after
- Apply it via Go's flag package to build CLIs that accept typed options, defaults, positional inputs, and missing-input checks.
Read how Learn Go with Pocket-Sized Projects explains flag package →
Go by Example
The flag package is Go's standard package for parsing command-line flags and defining their names, defaults, and value handling.
- How it is used
- Configures command-line tools by converting optional flags into variables that control program behavior.
- How deep it goes
- Explains FlagSet registration and parsing, then extends to positional arguments, usage customization, and flag.Value custom parsers.
- Examples
- Uses an HTTP request CLI with request-count and concurrency flags, including a million-request invocation.
- What you can do after
- Build it from scratch: create a CLI that registers typed flags, parses them, supplies defaults, and customizes parsing when needed.
Go in Action, Second Edition
The flag package is a Go standard-library package for defining, parsing, and retrieving command-line options and positional arguments.
- How it is used
- Configures flexible CLI tools by accepting options, feature toggles, and positional values at runtime.
- How deep it goes
- Defines typed flags, parsing, and positional-argument retrieval, but stops short of advanced aliases, subcommands, and automatic help.
- Examples
- Demonstrates help and banner flags, JSON/XML conversion, and positional input and output filenames.
- What you can do after
- Apply it via Go’s flag package to build configurable command-line programs with typed options and positional arguments.
Read how Go in Action, Second Edition explains flag package →
Related concepts
- command-line argumentuses · A command-line argument is a value supplied to a program when it is invoked, either as positional input or as an option that configures its behavior.
- flag.Parsehas part · flag.Parse is a Go flag-package function that parses command-line arguments according to defined flags and assigns their values to registered variables.
- value parserused by · A value parser is a function that converts a string flag value to a specified Go type, assigns the result to a variable, and reports parsing errors.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.



