Golang flag.StringVar Function

The flag.StringVar function in Go is used to define a string flag and bind it to an existing string 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 StringVar(p *string, name string, value string, usage string)

Parameters:

  • p: A pointer to the string 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.StringVar function does not return any value.

Example Usage

Basic Example

This example demonstrates how to use flag.StringVar to define a string flag and store its value in an existing variable.

package main

import (
	"flag"
	"fmt"
)

func main() {
	// Declare a string variable to hold the flag value
	var name string

	// Define the flag and bind it to the 'name' variable
	flag.StringVar(&name, "name", "World", "a name to say hello to")

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

	// Use the flag value stored in 'name'
	fmt.Printf("Hello, %s!\n", name)
}

Output:

If the program is run with go run main.go -name=Gopher:

Hello, Gopher!

If the program is run without any flags:

Hello, World!

Explanation:

  • The name variable is declared before calling flag.StringVar.
  • The flag.StringVar function associates the -name flag with the name variable, setting its default value to "World".
  • The flag.Parse function processes the command-line arguments and updates the name variable if the flag is provided.

Using Multiple Flags with flag.StringVar

You can define multiple string flags using flag.StringVar and bind them to different variables.

Example

package main

import (
	"flag"
	"fmt"
)

func main() {
	// Declare variables to hold the flag values
	var host string
	var port string

	// Define the flags and bind them to the variables
	flag.StringVar(&host, "host", "localhost", "hostname or IP address")
	flag.StringVar(&port, "port", "8080", "port number")

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

	// Use the flag values stored in 'host' and 'port'
	fmt.Printf("Connecting to %s:%s\n", host, port)
}

Output:

If the program is run with go run main.go -host=example.com -port=9090:

Connecting to example.com:9090

If the program is run without any flags:

Connecting to localhost:8080

Explanation:

  • Two variables, host and port, are declared to hold the values of the corresponding flags.
  • The flag.StringVar function associates the -host and -port flags with the respective variables.
  • The flag.Parse function processes the flags, and the program uses the values stored in host and port.

Default Values and Usage Messages

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

Example

package main

import (
	"flag"
	"fmt"
)

func main() {
	// Declare a string variable to hold the flag value
	var logLevel string

	// Define the flag and bind it to the 'logLevel' variable
	flag.StringVar(&logLevel, "loglevel", "info", "set the logging level (e.g., debug, info, warn, error)")

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

	// Use the flag value stored in 'logLevel'
	fmt.Printf("Logging level set to %s\n", logLevel)
}

If the program is run with go run main.go --help:

Output:

Usage of /tmp/go-build123456789/b001/exe/main:
  -loglevel string
        set the logging level (e.g., debug, info, warn, error) (default "info")

Explanation:

  • The flag.StringVar function sets the default logging level to "info" and 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.StringVar function in Go is a convenient way to define string 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.StringVar, 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