flag

CLI
import "flag"

Implements command-line flag parsing for building CLI applications.

Example

package main

import (
    "flag"
    "fmt"
)

func main() {
    port := flag.Int("port", 8080, "server port")
    verbose := flag.Bool("verbose", false, "verbose output")
    flag.Parse()
    fmt.Println(*port, *verbose)
}

Key Types & Functions

StringIntBoolParseArgsPrintDefaults

About flag

The flag package (imported as flag) belongs to the CLI category of Go packages. Implements command-line flag parsing for building CLI applications.

Go's standard library is one of the language's greatest strengths, providing production-ready implementations for networking, cryptography, encoding, I/O, and more. The flag package follows Go's philosophy of simplicity and composability — small, focused packages that combine through interfaces like io.Reader and io.Writer.

When using flag in production, follow Go best practices: handle errors explicitly, use context for cancellation and timeouts, prefer composition over inheritance, and write table-driven tests. The Go documentation at pkg.go.dev provides comprehensive API references and examples for every exported type and function.

Related Packages