Basic CLI with os.Args and flag
Context
Section titled “Context”Go provides two simple ways to read command‑line input:
os.Args– a slice of strings containing program name and arguments.flagpackage – parses Unix‑style flags (-name valueor--name value).
os.Args is good for positional arguments. flag is better for optional named flags.
Example
Section titled “Example”Use os.Args to print all arguments.
Code example
Section titled “Code example”package main
import ( "fmt" "os")
func main() { if len(os.Args) < 2 { fmt.Println("Usage: ./program <name>") return } name := os.Args[1] fmt.Printf("Hello, %s!\n", name)}Output
Section titled “Output”Hello, Alice!