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?

Popular General Dev topics Top

AstonJ
The obligatory speed test thread :smiley: Check here: https://www.speedtest.net When complete, click on the share link and copy and pas...
New
AstonJ
What chair do you have while working… and why? Is there a ‘best’ type of chair or working position for developers?
New
AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
New
AstonJ
poll poll Be sure to check out @Dusty’s article posted here: An Introduction to Alternative Keyboard Layouts It’s one of the best write-...
New
mafinar
I always start with excitement and then get busy on 9/10th day. This year, like the year before this, and the year before that, I intend ...
New
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
DevotionGeo
The V Programming Language Simple language for building maintainable programs V is already mentioned couple of times in the forum, but I...
New
First poster: OvermindDL1
Using Zig to Build Native Lua Scripts. Using Zig to Cross compile a Lua script for multiple arches
New
CommunityNews
9 fintech engineering mistakes. Read this list unless you want to build a money dissappearing system
New
New

Other popular topics Top

dasdom
No chair. I have a standing desk. This post was split into a dedicated thread from our thread about chairs :slight_smile:
New
AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
New
AstonJ
Just done a fresh install of macOS Big Sur and on installing Erlang I am getting: asdf install erlang 23.1.2 Configure failed. checking ...
New
Exadra37
I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
New
DevotionGeo
The V Programming Language Simple language for building maintainable programs V is already mentioned couple of times in the forum, but I...
New
AstonJ
Seems like a lot of people caught it - just wondered whether any of you did? As far as I know I didn’t, but it wouldn’t surprise me if I...
New
AstonJ
Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New
wmnnd
Here’s the story how one of the world’s first production deployments of LiveView came to be - and how trying to improve it almost caused ...
New
First poster: bot
The overengineered Solution to my Pigeon Problem. TL;DR: I built a wifi-equipped water gun to shoot the pigeons on my balcony, controlle...
New
New