Building the Basic Word Counter
Let’s create a tool that counts the number of words or lines provided as input using the standard input (STDIN) connection. By default, this tool will count the number of words, unless it receives the -l flag, in which case it’ll count the number of lines instead.
We’ll start by creating the basic implementation. This version reads data from STDIN and displays the number of words. We’ll eventually add more features, but this initial version will let you get comfortable with the code for a Go-based command-line application.
Before you dive into writing code for the word counter, let’s set up a project directory. In your home directory, create the subdirectory pragprog.com/rggo/firstProgram/wc and switch to it: ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access