Powerful Command-Line Applications in Go Book Club

Hello all.

Creating this space here for general discussion and chat about Powerful Command-Line Applications In Go

In particular, we can use this topic as an entry point to share and discuss solutions to the book’s exercises. Thanks @adamwoolhether for the suggestion!

7 Likes

Corresponding tweet for this thread:

Share link for this tweet.

2 Likes

I am going through the book without knowing any go programming.

“Exercises
You can improve your understanding of the concepts discussed here by doing these exercises:

Add another command-line flag, -b, to count the number of bytes in addition to words and lines.

Then, update the count function to accept another parameter, countBytes. When this input parameter is set to true, the function should count bytes. (Hint: check all the methods available for the type bufio.Scanner in the Go documentation.[12])

Write tests to ensure the new feature works as intended.”

Excerpt From: Ricardo Gerardi. “Powerful Command-Line Applications in Go.”

Test:
cat main_test.go

package main

import (
	"bytes"
	"testing"
)

func TestCountWords(t *testing.T) {
	b := bytes.NewBufferString("word1 word2 word3 word4\n")
	exp := 4
	res := count(b, false, false)
	if res != exp {
		t.Errorf("Expected %d, got %d instead.\n", exp, res)
	}
}

func TestCountLines(t *testing.T) {
	b := bytes.NewBufferString("word1 word2 word3\nline2\nline3 word1")
	exp := 3
	res := count(b, true, false)
	if res != exp {
		t.Errorf("Expected %d, got %d instead.\n", exp, res)
	}
}

func TestCountBytes(t *testing.T) {
	b := bytes.NewBufferString("word1 word2 word3\n")
	exp := 18
	res := count(b, false, true)
	if res != exp {
		t.Errorf("Expected %d, got %d instead.\n", exp, res)
	}
}

cat main.go

package main

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

func main() {
	lines := flag.Bool("l", false, "Count lines")
	bytes := flag.Bool("b", false, "Count bytes")
	flag.Parse()
	fmt.Println(count(os.Stdin, *lines, *bytes))

}

func count(r io.Reader, countLines bool, countBytes bool) int {

	scanner := bufio.NewScanner(r)
	if !countLines && !countBytes {
		scanner.Split(bufio.ScanWords)
	}
	if countBytes {
		scanner.Split(bufio.ScanBytes)
	}
	wc := 0

	for scanner.Scan() {
		wc++
	}
	return wc
}
3 Likes

Looks good to me @Fernando , great work there. There are other ways to solve this exercise and, if you’re just starting with Go, you’ll see them as you read through the book.

3 Likes

Hi @rgerardi, thanks again for setting this up.

I’ll pop in and help reply to questions for exercises I’ve solved. I can get most of them, but am at a loss with #2 & #3 for Chapter 2.

I’m assuming that solving these requires implement the Formatter interface, and calling it in main with fmt.Printf?

Or do I write another custom method to call in the case that my verbose or incomplete flag bools are selected?

3 Likes

Hey @adamwoolhether . Sounds good. I will be looking for your exercises solutions.

For Chapter 2 #2 and #3, creating a custom method is what I had in mind, mainly due to the way we structured the library. The exercise’s main goal was to practice adding new flags, more than the implementation.

To increase code re-usability, you can create a non-exported method that prints different versions of the list according to given parameters, and a couple of exported methods wrapping the non-exported one with the appropriate parameters, depending on the flags the user provided.

Please let me know if this helps.

Ricardo

3 Likes