Command-Line Flags

This Go program showcases the usage of the flag package to parse command-line arguments. Let's go through the code with inline comments:

package main

import (
	"flag"
	"fmt"
)

func main() {
	// Define flags using the flag package
	wordPtr := flag.String("word", "foo", "a string")
	numbPtr := flag.Int("numb", 42, "an int")
	forkPtr := flag.Bool("fork", false, "a bool")

	// You can also define a flag and bind it to an existing variable using flag.StringVar
	var svar string
	flag.StringVar(&svar, "svar", "bar", "a string var")

	// Parse the command-line arguments
	flag.Parse()

	// Print the values of the flags and other non-flag arguments
	fmt.Println("word:", *wordPtr)
	fmt.Println("numb:", *numbPtr)
	fmt.Println("fork:", *forkPtr)
	fmt.Println("svar:", svar)
	fmt.Println("tail:", flag.Args())
}

Explanation:

  1. Defining Flags:

    • The program defines four flags using the flag package: wordPtr, numbPtr, forkPtr, and svar.

  2. Setting Default Values:

    • Default values are specified for each flag using the String, Int, and Bool functions.

  3. Binding to Existing Variables:

    • The program demonstrates how to bind a flag to an existing variable using flag.StringVar.

  4. Parsing Flags:

    • flag.Parse() is called to parse the command-line arguments.

  5. Printing Values:

    • The program prints the values of the flags and any non-flag arguments (referred to as "tail").

When you run this program with various command-line arguments, the values of the flags and the remaining arguments will be printed. For example:

go run main.go -word=hello -numb=7 -fork -svar=world arg1 arg2

Output:

word: hello
numb: 7
fork: true
svar: world
tail: [arg1 arg2]

You can experiment with different command-line arguments and see how the values of the flags change.

Last updated