Go flag package: command-line flags
Answer: Package flag implements command-line flag parsing. Package flag implements command-line flag parsing.
Examples
package main
import (
"flag"
"fmt"
)
func main() {
name := flag.String("name", "world", "who to greet")
flag.Parse()
fmt.Println(*name)
}
Rules
- Parse after all flags are defined and before the values are used.
- Flag names should be unique in the default
CommandLineset.
See also
Claims
- id: C1 claim: Package flag implements command-line flag parsing. source: S1 evidence: "Package flag implements command-line flag parsing"
- id: C2 claim: The package is documented on pkg.go.dev. source: S1 evidence: "Package flag implements command-line flag parsing"
- id: C3 claim: The Go source describes the same flag package. source: S2 evidence: "Package flag implements command-line flag parsing"
Sources
- [S1] flag package. Go docs. https://pkg.go.dev/flag . License: BSD-3-Clause. Accessed 2026-11-18.
- [S2] src/flag/flag.go. Go source. https://raw.githubusercontent.com/golang/go/master/src/flag/flag.go . License: BSD-3-Clause. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Go docs, flag package; Go source, src/flag/flag.go. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Go docs, flag package; Go source, src/flag/flag.go. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors.
Cite this
iinventedlight. 2026. "Go flag package: command-line flags." iinventedlight Library, iil:note:4433d6m5, version of 2026-11-18. https://iinventedlight.com/library/notes/go-flag/. License: CC-BY-4.0.
@misc{iil_note_4433d6m5,
author = {{iinventedlight}},
title = {{Go flag package: command-line flags}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/go-flag/},
note = {Permanent ID iil:note:4433d6m5. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/go-flag.md.