Golang flag.Int Function

The flag.Int function in Go is used to define an integer flag and return a pointer to the integer variable that stores the flag’s value. This function is part of the flag package and is useful when you want to define a command-line flag and directly retrieve its value without binding it to a pre-declared variable.

Syntax

func Int(name string, value int, usage string) *int

Parameters:

  • 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:

  • *int: A pointer to an integer variable that holds the value of the flag.

Example Usage

Basic Example

This example demonstrates how to use flag.Int to define an integer flag and retrieve its value.

package main

import (
	"flag"
	"fmt"
)

func main() {
	// Define the flag and get a pointer to the integer variable
	port := flag.Int("port", 8080, "port number to connect to")

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

	// Use the flag value by dereferencing the pointer
	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 flag.Int function defines a -port flag with a default value of 8080 and returns a pointer to the integer variable that stores the flag’s value.
  • The flag.Parse function processes the command-line arguments and updates the value of the port variable if the flag is provided.
  • The program then uses the flag value by dereferencing the pointer (*port).

Using Multiple Flags with flag.Int

You can define multiple integer flags using flag.Int and retrieve their values through the returned pointers.

Example

package main

import (
	"flag"
	"fmt"
)

func main() {
	// Define multiple flags and get pointers to the integer variables
	min := flag.Int("min", 1, "minimum value")
	max := flag.Int("max", 100, "maximum value")

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

	// Use the flag values by dereferencing the pointers
	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:

  • The flag.Int function is used to define the -min and -max flags, returning pointers to the respective integer variables.
  • The flag.Parse function processes the command-line arguments, and the program uses the values by dereferencing the pointers.

Default Values and Usage Messages

The default values and usage messages provided in flag.Int are shown when the user requests help (e.g., by using -h or --help).

Example

package main

import (
	"flag"
	"fmt"
)

func main() {
	// Define an integer flag and get a pointer to the integer variable
	retries := flag.Int("retries", 3, "number of retries before failing")

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

	// Use the flag value by dereferencing the pointer
	fmt.Printf("Retry count set to %d\n", *retries)
}

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.Int function defines a -retries flag with a default value of 3 and a usage description.
  • When the user requests help, the usage message is displayed along with the default value.

Accessing Non-Flag Arguments

After calling flag.Parse, any command-line arguments that are not associated with flags can be accessed using the flag.Args function.

Example

package main

import (
	"flag"
	"fmt"
)

func main() {
	// Define an integer flag and get a pointer to the integer variable
	limit := flag.Int("limit", 10, "maximum number of items to process")

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

	// Access non-flag arguments
	nonFlagArgs := flag.Args()

	// Use the flag value and non-flag arguments
	fmt.Printf("Processing up to %d items for %v\n", *limit, nonFlagArgs)
}

Output:

If the program is run with go run main.go -limit=5 file1 file2:

Processing up to 5 items for [file1 file2]

Explanation:

  • The flag.Args function returns a slice of non-flag arguments provided on the command line.
  • The program uses the flag value and the non-flag arguments to determine how many items to process.

Conclusion

The flag.Int function in Go is a convenient way to define integer flags and retrieve their values directly through pointers. This approach simplifies the management of command-line arguments, allowing you to set default values, provide descriptive usage messages, and efficiently handle user input. By using flag.Int, you can create flexible and user-friendly command-line interfaces in your Go applications.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top