Command-line arguments revisited!
As we saw in Chapter 2,Writing Programs in Go, you cannot work efficiently with multiple command-line arguments and options using if
statements. The solution to this problem is to use the flag
package, which will be explained here.
Note
Remembering that the flag
package is a standard Go package and that you do not have to search for the functionality of a flag elsewhere is extremely important.
The flag package
The flag
package does the dirty work of parsing command-line arguments and options for us; so, there is no need for writing complicated and perplexing Go code. Additionally, it supports various types of parameters, including strings, integers, and Boolean, which saves you time as you do not have to perform any data type conversions.
The usingFlag.go
program illustrates the use of the flag
Go package and will be presented in three parts. The first part has the following Go code:
package main import ( "flag" "fmt" )
The second part, which has the...