Go provides a built-in library called flag for writing CLI tools. It refers to the command-line flags. Since it is already packed with the Go distribution, there is no need to install anything externally. The flag package has multiple functions, such as Int and String, to handle the respective type input that's supplied as a command-line flag. Let's suppose that we collect a name from the user and print it back to the console.
To do this, we can use the flag.String method, as shown in the following code snippet:
import "flag"
var name = flag.String("name", "stranger", "your wonderful name")
Let's write a short program to illustrate the flag API in more detail:
- Create a file called flagExample.go in the GOPATH, as follows:
mkdir -p $GOPATH/src/github.com/git-user/chapter8/basic...