Go - Flag (Argument Parsing)

Card Puncher Data Processing


The Flag package to parse Go arguments passed to entry point of the application (main)

How to

To change the usage output, you need to overwrite the Usage variable.

Example to add an header and a footer

flag.Usage = func() {
	fmt.Fprintln(os.Stderr, "An header")
	fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
	fmt.Fprintln(os.Stderr, "A footer")

Get the arguments without flag (the last arguments)

The non-flag arguments are available from flag.Args()

fmt.Println("tail:", flag.Args())

Documentation / Reference

Share this page:
Follow us:
Task Runner