0
0
GoHow-ToBeginner · 3 min read

How to Use the flag Package in Go for Command-Line Arguments

Use the flag package in Go to define command-line flags with functions like flag.String, flag.Int, and then call flag.Parse() to read the values. Access the flag values through pointers returned by these functions after parsing.
📐

Syntax

The flag package lets you define flags with a name, default value, and description. You create flags using functions like flag.String, flag.Int, and flag.Bool. Each returns a pointer to the variable holding the flag value. After defining all flags, call flag.Parse() to process the command-line input.

  • flag.String(name, default, usage): defines a string flag
  • flag.Int(name, default, usage): defines an integer flag
  • flag.Bool(name, default, usage): defines a boolean flag
  • flag.Parse(): parses the flags from command-line arguments
go
package main

import (
	"flag"
	"fmt"
)

func main() {
	name := flag.String("name", "Guest", "Your name")
	age := flag.Int("age", 0, "Your age")
	verbose := flag.Bool("verbose", false, "Enable verbose output")

	flag.Parse()

	fmt.Println("Name:", *name)
	fmt.Println("Age:", *age)
	fmt.Println("Verbose mode:", *verbose)
}
💻

Example

This example shows how to define and parse three flags: a string name, an integer age, and a boolean verbose. After parsing, it prints the values. Run the program with flags like -name=Alice -age=30 -verbose=true.

go
package main

import (
	"flag"
	"fmt"
)

func main() {
	name := flag.String("name", "Guest", "Your name")
	age := flag.Int("age", 0, "Your age")
	verbose := flag.Bool("verbose", false, "Enable verbose output")

	flag.Parse()

	fmt.Printf("Hello, %s!\n", *name)
	fmt.Printf("You are %d years old.\n", *age)
	if *verbose {
		fmt.Println("Verbose mode is ON.")
	} else {
		fmt.Println("Verbose mode is OFF.")
	}
}
Output
Hello, Alice! You are 30 years old. Verbose mode is ON.
⚠️

Common Pitfalls

Common mistakes when using the flag package include:

  • Not calling flag.Parse() before accessing flag values, so flags remain at default values.
  • Accessing flag variables directly instead of dereferencing pointers (use *flagVar).
  • Defining flags after calling flag.Parse(), which has no effect.
  • Confusing flag names with positional arguments; flags must start with a dash -.
go
package main

import (
	"flag"
	"fmt"
)

func main() {
	name := flag.String("name", "Guest", "Your name")

	// Wrong: Accessing before parsing
	fmt.Println("Name before parse:", *name) // Always "Guest"

	flag.Parse()

	// Correct: Access after parsing
	fmt.Println("Name after parse:", *name)
}
Output
Name before parse: Guest Name after parse: Alice
📊

Quick Reference

FunctionDescriptionExample
flag.String(name, default, usage)Defines a string flag`name := flag.String("name", "Guest", "Your name")`
flag.Int(name, default, usage)Defines an integer flag`age := flag.Int("age", 0, "Your age")`
flag.Bool(name, default, usage)Defines a boolean flag`verbose := flag.Bool("verbose", false, "Verbose mode")`
flag.Parse()Parses command-line flags`flag.Parse()`
Access flag valueDereference the pointer`fmt.Println(*name)`

Key Takeaways

Always define flags before calling flag.Parse() to read command-line arguments.
Access flag values by dereferencing the pointers returned by flag functions.
Call flag.Parse() once to process all flags before using their values.
Use descriptive flag names and provide default values and usage messages.
Remember flags start with a dash (-) when running your Go program.