import ( "flag" "fmt" ) func main() { fs := flag.NewFlagSet("example", flag.ExitOnError) var name string fs.StringVar(&name, "name", "", "name to say hello to") fs.Parse([]string{"--name", "John"}) fmt.Printf("Hello, %s!\n", name) }
import ( "flag" "fmt" ) func main() { fs := flag.NewFlagSet("example", flag.ExitOnError) var verbose bool fs.BoolVar(&verbose, "verbose", false, "enable verbose logging") fs.Parse([]string{"--verbose"}) if verbose { fmt.Println("Verbose logging enabled!") } }In this example, we use `BoolVar` to bind a boolean variable named "verbose" to the flag "verbose" on the command line. We then call `Parse` with an array of arguments and print out a message if the `verbose` flag is set to `true`. Both examples above use the `flag` package that is part of the Go standard library.