Skip to content

Instantly share code, notes, and snippets.

View adilw3nomad's full-sized avatar

adilw3nomad adilw3nomad

  • Somewhere
View GitHub Profile

29 May 2019 at 05:20

I started my morning with some cowboy coffee; which is coffee brewed in a saucepan, since I seem to have misplaced my aero press, as I listened to Go for beginners, and discovered Gophercises, a set of coding exercises in Go aimed at people who’ve done a few tutorials but have kinda lost steam. I felt like that described me, so I’ve decided to tackle these exercises while journalling my progress and learning in this blog.

/I have a peculiar writing style because I write in tandem with coding, so there’s a lot of tense switching, so apologies in advance. All comments and criticisms are encouraged, no matter how harsh or abrasive, as long as they are constructive!/

I’m going to tackle the first challenge, GitHub - gophercises/quiz: Ex 1 - Run timed quizzes via the command line.

So the first thing I need to do is make a new go pr

package main

import (
"bufio"
"encoding/csv"
"fmt"
"log"
"os"
"strings"

2 Jun 2019 at 01:52


We finished the last post with three tasks to tackle:

  • Adding tests
  • Keeping track of the score
  • Adding a flag that allows the CSV file to be customised.

The priority is to add tests, as I’m a firm believer in the benefits of Test Driven Development, often abbreviated as TDD. In a nutshell, you write a test of some behaviour you want to implement. You then run the test, see it fail, and make the smallest possible change to make it pass. Once your test is green, it’s time to refactor. Thus it becomes a cycle; red green, refactor.

shell
06:56 $ go test
PASS
ok github.com/adilw3nomad/GopherQuiz 0.004s
func CheckAnswer(answer string, correctAnswer string) string {
if answer == correctAnswer {
return “Correct! Well done”
} else {
return (“WRONG! Answer is: “ + correctAnswer)
}
}
shell
06:51 $ go test
--- FAIL: TestCheckIncorrectAnswer (0.00s)
main_test.go:15: CheckIncorrectAnswer = Correct! Well done; want 'WRONG! Answer is: 2'
FAIL
exit status 1
FAIL github.com/adilw3nomad/GopherQuiz 0.005s
go
func CheckAnswer(string, string) string {
return "Correct! Well done"
}
shell
06:47 $ go test
--- FAIL: TestCheckCorrectAnswer (0.00s)
main_test.go:8: CheckCorrectAnswer = ; want 'Correct! Well Done'
--- FAIL: TestCheckIncorrectAnswer (0.00s)
main_test.go:15: CheckIncorrectAnswer = ; want 'WRONG! Answer is: 2'
FAIL
exit status 1
FAIL github.com/adilw3nomad/GopherQuiz 0.005s
go
func CheckAnswer(string, string) string {
return ""
}
shell
06:36 $ go test
# github.com/adilw3nomad/GopherQuiz [github.com/adilw3nomad/GopherQuiz.test]
./main_test.go:7:9: cannot convert "Correct! Well done" (type untyped string) to type int
./main_test.go:7:9: invalid operation: got != "Correct! Well done" (mismatched types int and string)
./main_test.go:14:9: cannot convert "WRONG! Answer is: 2" (type untyped string) to type int
./main_test.go:14:9: invalid operation: got != "WRONG! Answer is: 2" (mismatched types int and string)
FAIL github.com/adilw3nomad/GopherQuiz [build failed]