rgerardi

rgerardi

Author of Powerful Command-Line Applications in Go

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!

Most Liked

Fernando

Fernando

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
}
rgerardi

rgerardi

Author of Powerful Command-Line Applications in Go

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.

adamwoolhether

adamwoolhether

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?

Where Next?

Popular Community topics Top

Rainer
My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
New
finner
As one of my New Year resolutions is to read more tech I’ve decided on an attempt to document my travels in Mannings Modern Java in Actio...
New
Tommy
So I have enough money to last a year. Realistically I’m still going to have to work part time painting. I’m so done with it though! I h...
New
mafinar
Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
New
mafinar
I am going to dump my thoughts, methods, codes, experiences and rants while learning OCaml into this thread. This is probably the 5th or...
New
mafinar
TL;DR I am reading “Domain Modeling Made Functional” and discussing and keeping a journal of what I learned from it, any co-readers welco...
New
ohm
I would love to begin a book club with Mike Amundsen’s (@mamund) book Design and Build Great Web APIs. It seems that building new syste...
New
RomanTurner
Agile Web Development with Rails 6 Chapter 11. Task F Currently reading and working through AWDR6 by Sam Ruby, David Bryant Copeland, a...
New
adamaiken89
Anyone is interested in a classical textbook for algorithms can go and check that.
New
PragmaticBookshelf
When the pandemic, heart disease, and personal tragedy threatened to steal everything the Tates spent years building, they found hope, he...
New

Other popular topics Top

AstonJ
What chair do you have while working… and why? Is there a ‘best’ type of chair or working position for developers?
New
ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
AstonJ
Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face: Perhaps if there’s enough peop...
New
Rainer
My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
New
PragmaticBookshelf
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
New
Exadra37
On modern versions of macOS, you simply can’t power on your computer, launch a text editor or eBook reader, and write or read, without a ...
New
AstonJ
I have seen the keycaps I want - they are due for a group-buy this week but won’t be delivered until October next year!!! :rofl: The Ser...
New
Margaret
Hello content creators! Happy new year. What tech topics do you think will be the focus of 2021? My vote for one topic is ethics in tech...
New
mafinar
Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
New
PragmaticBookshelf
Author Spotlight: Sophie DeBenedetto @SophieDeBenedetto The days of the traditional request-response web application are long gone, b...
New