Golang Flag Package
Overview
The golang flag is used for developing a UNIX system program that takes in the command line argument for manipulating the program's behavior. The flag is a build-in package in golang that is shipped with the standard library of golang. Arguments can be passed to the program on start as flags. The flag is a string that is formatted and is then passed to an argument of a program so that according to the flag that is passed, the control of the behavior of the program has few arguments utilities.
Introduction to Golang Flag
The terminal commands that are used in UNIX or windows systems have golang flags that are associated with them. These flags in windows systems are called switches whereas, in UNIX or Linux systems, these are called flags only. The function of the flag is however same in both systems which are to control the behavior of the program during the runtime. In most of these platforms, the utilities of the command line accept the flag for customizing the execution of the command line.
The flags are string having key-value pair and is added after the command name. The golang however, helps in crafting the command line utilities by using the flag package derived from the standard library.
Golang Flag Syntax
For writing the golang flag, there are different options available few of which are as follows:
- -count=x
- -count x
- --count=x // double dashes are also permitted
- --count x
The syntax given is for all the types except for the boolean. The boolean flags have the below options:
- -upper
- --upper
- -upper=value
- --upper=value
Golang Flag Example
To understand the concept of the golang flag better, look at the example given below: In this example, we will write a simple program to parse an integer argument.
The file will be named main.go
RUN
OUTPUT
Explanation: In this example, the code is executed and the word “helloworld” is printed n times which in this case is 5 times. Here the value n is parsed from the arguments of the command line. If n is not passed, then it will run 1 time, as it is set as a default value.
The first step is to import all the packages. The next step is to register an int flag using the flag package. In the argument of the flag, the first parameter is the name of the flag and the second parameter is the default value of the flag followed by the flag description. The next step after this is to parse the flag by using the method flag.Parse().
Flag.string and Flag.int Golang
In golang, the flag.String is used for defining the string flag with a specified name, the default value of the string, and its usage. The return value of this function is the address of the string variable that stores the value of the respective string flag. The flag.Int, however, is used for defining an int flag with the specified name, the default value, and the respective string usage. Here the return value of this function is the address of the variable int whose value is stored in the flag. In order to understand this better, let's take an example.
File name: main.go
Explanation: In this example, the port works with two flags, the first is the environment option and the second is the port number. The statements are in the init function where the state of the variable is initialized.
Output:
Golang Flag.stringVar
The flag.stringVar is used for defining a string flag with a specified name, and default value as well as the usage of string. The first argument which is the specified name points to a string variable in which the value of the flag is stored. To understand this better, let's take an example.
The file is named main.go
Explanation: In this example, we will take the name as input from the user through the command line.
Golang Flag.printdefaults
The flag.PrintDefaults prints a usage message showing the default settings of all defined command-line flags.
In order to understand it better, let’s look at the example given below
Explanation: In this example, the program registers the flag name and makes it as required. If the variable, name is empty then the usage of the program is printed.
The file is saved as main.go
Output:
Golang Flag.boolvar
The flag.BoolVar is used for defining a flag with a boolean value. This flag has a specified name, default value, as well as string usage. The first argument of this flag points to a boolean value variable which is used for storing the value of a flag.
To understand these better, let's take an example given below, the file name is saved as main.go
Explanation: In this example, the code takes as input the name of the user, and a boolean flag is used for checking if the message is to be written in upper case.
Output:
Flag.args Golang
In this example, the arguments that are following the flag are available in the slice by using flag.Args or by using flag.Arg(i) for individual elements. The arguments in this are indexed from 0 to flag.NArg() - 1 which is the last element of the slice.
To understand this better, let's take the example given below and save the file as main.go
Explanation: In this example, the code has flag u that is used for printing all the words that are following the flag to upper case. The next step is to define the boolean flag and then get all the flags from the terminal by using the for a loop. We then go through the slice and print all the word that is set in the upper case flag.
Output:
Examples for Understanding
To understand all about flags or whatever we have learned till now, we will take an example that uses all the flag utilities.
Explanation: In this example, we can observe that the code has two command line options, -u which is for username, and -p is for password. The statement flag.String(“u”, “root”, “username”) is used for defining a string option for the command line.
The username -u is defined with a default value of the root. When the code is executed, without any argument, the default value of the flag is printed in the output terminal.
Output
But when the code is executed with arguments in the command line then those values are used in the flag. For instance,
Output
Conclusion
- In golang, the flag package is used for developing a UNIX system program that takes in the command line argument for manipulating the program's behavior. The flag is a build-in package in golang that is shipped with the standard library of golang.
- The golang flag is a string that is formatted and is then passed to an argument of a program so that according to the flag that is passed, the control of the behavior of the program has few arguments utilities.
- The terminal commands that are used in UNIX or windows systems have flags that are associated with them. These flags in windows systems are called switches whereas, in UNIX or Linux systems, these are called flags only. The function of the flag is however same in both systems which are to control the behavior of the program during the runtime.
- In most of these platforms, the utilities of the command line accept the flag for customizing the execution of the command line. The golang flag are string having key-value pair and is added after the command name. The golang however, helps in crafting the command line utilities by using the flag package derived from the standard library.