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:

 $ ​​mkdir​​ ​​-p​​ ​​$HOME/pragprog.com/rggo/firstProgram/wc
 $ ​​cd​​ ​​$HOME/pragprog.com/rggo/firstProgram/wc

Go programs are composed of packages. A package consists of one or more Go source code files with code that can be combined into executable programs or libraries.

Starting with Go 1.11, you can combine one or more packages into Go modules. Modules are a new Go standard for grouping related packages into a single unit that can be versioned together. Modules enable consistent dependency management for your Go applications. For more information about Go modules, consult the official wiki page.[10]

Initialize a new Go module for your project:

 $ ​​go​​ ​​mod​​ ​​init​​ ​​pragprog.com/rggo/firstProgram/wc
 go: creating new go.mod: module pragprog.com/rggo/firstProgram/wc

You create an executable program in Go by defining a package named main that contains a function called main. This function takes no arguments and returns no values. It serves as the entry point for your program.

 package​ main
 
 func​ main() {
  main contents
 }

Although not a requirement, by convention, the main package is usually defined in a file named main.go. You’ll use this convention throughout this book.

Code Example File Path

images/aside-icons/important.png

For brevity, the code example path omits the root directory $HOME/pragprog.com/rggo. For example, in the following code sample, the code path starts at firstProgram/wc.

Create the file main.go using your favorite text editor. Add the package main definition to the top of the file like this:

 package​ main

Next, add the import section to bring in the libraries you’ll use to read data from STDIN and print results out.

 import​ (
 "bufio"
 "fmt"
 "io"
 "os"
 )

For this tool, you import the bufio package to read text, the fmt package to print formatted output, the io package which provides the io.Reader interface, and the os package so you can use operating system resources.

Your word counter will have two functions: main and count. The main function is the starting point of the program. All Go programs that will be compiled into executable files require this function. Create this function by adding the following code into your main.go file. This function will call the count function and print out that function’s return value using the fmt.Println function:

 func​ main() {
 // Calling the count function to count the number of words
 // received from the Standard Input and printing it out
  fmt.Println(count(os.Stdin))
 }

Next, define the count function, which will perform the actual counting of the words. This function receives a single input argument: an io.Reader interface. You’ll learn more about Go interfaces in Chapter 2, Interacting with Your Users. For now, think of an io.Reader as any Go type from which you can read data. In this case, the function will receive the contents of the STDIN to process.

 func​ count(r io.Reader) ​int​ {
 // A scanner is used to read text from a Reader (such as files)
  scanner := bufio.NewScanner(r)
 
 // Define the scanner split type to words (default is split by lines)
  scanner.Split(bufio.ScanWords)
 
 // Defining a counter
  wc := 0
 // For every word scanned, increment the counter
 for​ scanner.Scan() {
  wc++
  }
 
 // Return the total
 return​ wc
 }

The count function uses the NewScanner function from the bufio package to create a new scanner. A scanner is a convenient way of reading data delimited by spaces or new lines. By default, a scanner reads lines of data, so we instruct the scanner to read words instead by setting the Split function of the scanner to bufio.ScanWords. We then define a variable, wc, to hold the word count and increment it by looping through each token using the scanner.Scan function and adding 1 to the counter each time. We then return the word count.

In this example, for simplicity’s sake, we are ignoring the error that may be generated during the scanning. In your code, always check for errors. You’ll learn more about dealing with errors in the context of a command-line tool in Creating the Initial To-Do Command-Line Tool.

You’ve completed the basic implementation of the word count tool. Save the file main.go with your changes. Next, you’ll write tests to ensure this implementation works the way you expect it to.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.104.97