avatarDavid Lee

Summarize

Build Your First CLI tool in Golang

Source Code:

This Go program is like a bouncer at a club, but instead of counting people, it’s counting words. The club, in this case, is the standard input (os.Stdin), and the bouncer is our count function.

The count function starts by hiring a scanner from the bufio package. This scanner is like a bouncer's clicker, counting each word that comes in. The scanner is told to split the input into words using scanner.Split(bufio.ScanWords). This is like telling the bouncer to count individuals, not groups.

Then we have a counter, wc, which starts at 0 - the club is empty at the start of the night.

The for scanner.Scan() loop is the bouncer's shift. For each word (or club-goer) that comes in, the bouncer clicks the counter (wc++). This continues until the club closes (or the input ends).

Finally, the bouncer reports the total count (return wc). This is like the bouncer telling the club owner how many people came in that night.

And there you have it! A Go program that’s moonlighting as a club bouncer for words. Remember, no ID, no entry… or in this case, no word, no count!

Golang
Programming
Go
Terminal
Cli
Recommended from ReadMedium