December 2021
Intermediate to advanced
510 pages
11h 20m
English
Good command-line tools provide flexibility through options. The current version of the word counter tool counts words. Let’s add the ability to count lines as well by giving the user the option to decide when to switch this behavior through command-line flags.
Go provides the flag package, which you can use to create and manage command-line flags. You’ll learn about it in more detail in Handling Multiple Command-Line Options. For now, open the main.go file and add this package to your imports section:
| | import ( |
| | "bufio" |
| » | "flag" |
| | "fmt" |
| | "io" |
| | "os" |
| | ) |
Next, update the main function by adding the definition for the new command-line flag:
| | func main() { |
| | // Defining a boolean flag -l to count iines ... |
Read now
Unlock full access