How to use flag package

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".