Using command-line flags
The flag
package makes it simple to add command-line flag arguments to a Go application. It has a few shortcomings--you tend to duplicate a lot of code in order to add shorthand versions of flags, and they're ordered alphabetically from the help prompt. There are a number of third-party libraries that attempt to address these shortcomings, but this chapter will focus on the standard library version and not on those libraries.
Getting ready
Configure your environment according to these steps:
- Download and install Go on your operating system from https://golang.org/doc/install, and configure your
GOPATH
environment variable: - Open a terminal/console application, and navigate to your
GOPATH/src
and create a project directory, for example,$GOPATH/src/github.com/yourusername/customrepo
.
All code will be run and modified from this directory.
- Optionally, install the latest tested version of the code using the
go get github.com/agtorre/go-cookbook/
command.
How to do it...
These...