Import flag, define variables, bind them with flag functions, call flag.Parse(), and use the variables in your code.
Import the flag package, define variables, bind them to flags using functions like flag.StringVar, call flag.Parse(), then access the variables.
package main
import (
"flag"
"fmt"
)
func main() {
var name string
flag.StringVar(&name, "name", "world", "A greeting name")
flag.Parse()
fmt.Println("Hello", name)
}
Run with go run main.go -name=alice to see "Hello alice".
The flag package lets your program accept custom settings from the command line, like turning on a debug mode or specifying a filename. Think of it as a way to give your program instructions without changing the code itself. You define what options are allowed, and the package handles reading them when the user runs the program.