The flag.IntVar function in Go is used to define an integer flag and bind it to an existing integer variable. This function is part of the flag package and allows you to directly store the value of a command-line flag into a variable that you have already declared.
Syntax
func IntVar(p *int, name string, value int, usage string)
Parameters:
p: A pointer to the integer variable where the flag’s value should be stored.name: The name of the command-line flag (e.g.,-flagname).value: The default value of the flag if it is not provided by the user.usage: A short description of the flag’s purpose, which is shown in the help output.
Returns:
- The
flag.IntVarfunction does not return any value.
Example Usage
Basic Example
This example demonstrates how to use flag.IntVar to define an integer flag and store its value in an existing variable.
package main
import (
"flag"
"fmt"
)
func main() {
// Declare an integer variable to hold the flag value
var port int
// Define the flag and bind it to the 'port' variable
flag.IntVar(&port, "port", 8080, "port number to connect to")
// Parse the command-line flags
flag.Parse()
// Use the flag value stored in 'port'
fmt.Printf("Connecting to port %d\n", port)
}
Output:
If the program is run with go run main.go -port=9090:
Connecting to port 9090
If the program is run without any flags:
Connecting to port 8080
Explanation:
- The
portvariable is declared before callingflag.IntVar. - The
flag.IntVarfunction associates the-portflag with theportvariable, setting its default value to8080. - The
flag.Parsefunction processes the command-line arguments and updates theportvariable if the flag is provided.
Using Multiple Flags with flag.IntVar
You can define multiple integer flags using flag.IntVar and bind them to different variables.
Example
package main
import (
"flag"
"fmt"
)
func main() {
// Declare variables to hold the flag values
var min int
var max int
// Define the flags and bind them to the variables
flag.IntVar(&min, "min", 1, "minimum value")
flag.IntVar(&max, "max", 100, "maximum value")
// Parse the command-line flags
flag.Parse()
// Use the flag values stored in 'min' and 'max'
fmt.Printf("Range: %d - %d\n", min, max)
}
Output:
If the program is run with go run main.go -min=10 -max=50:
Range: 10 - 50
If the program is run without any flags:
Range: 1 - 100
Explanation:
- Two variables,
minandmax, are declared to hold the values of the corresponding flags. - The
flag.IntVarfunction associates the-minand-maxflags with the respective variables. - The
flag.Parsefunction processes the flags, and the program uses the values stored inminandmax.
Default Values and Usage Messages
The default values and usage messages provided in flag.IntVar are shown when the user requests help (e.g., by using -h or --help).
Example
package main
import (
"flag"
"fmt"
)
func main() {
// Declare an integer variable to hold the flag value
var retryCount int
// Define the flag and bind it to the 'retryCount' variable
flag.IntVar(&retryCount, "retries", 3, "number of retries before failing")
// Parse the command-line flags
flag.Parse()
// Use the flag value stored in 'retryCount'
fmt.Printf("Retry count set to %d\n", retryCount)
}
If the program is run with go run main.go --help:
Output:
Usage of /tmp/go-build123456789/b001/exe/main:
-retries int
number of retries before failing (default 3)
Explanation:
- The
flag.IntVarfunction sets the default number of retries to3and provides a usage message that describes the purpose of the flag. - When the user requests help, the usage message is displayed along with the default value.
Conclusion
The flag.IntVar function in Go is a convenient way to define integer flags and bind them directly to existing variables. This approach makes it easy to manage and use command-line arguments in your programs, allowing you to set default values, provide descriptive usage messages, and handle input from users efficiently. By using flag.IntVar, you can create flexible and user-friendly command-line interfaces in your Go applications.