So, I'm pretty new to both Go and concurrency (I've been programming with other languages like C# and Python for some time but never learned concurrency/multi-threaded programming, only ever used "async/await" stuff which is quite different).
I'm going through , the first exercise which is about making a quiz with a timer.
This is the solution I came up with myself, and it is pretty different from the solution of the author (Jon Calhoun).
My code "works", not perfectly but it works... I've asked ChatGPT and read through it's answer but I still can not really understand why mine is not an optimal solution.
Could you take a look and help me out?
package main
import (
"encoding/csv"
"flag"
"fmt"
"log"
"os"
"strings"
"time"
)
func main() {
csvProvided := flag.String("csv", "problems.csv", "csv file containing problem set")
timeLimitProvided := flag.Int("time", 5, "time limit")
flag.Parse()
// Open the CSV file
csvFile, err := os.Open(*csvProvided)
if err != nil {
log.Fatalf("Error opening csv file: %v", err)
}
defer csvFile.Close()
// Read the CSV data
reader := csv.NewReader(csvFile)
data, err := reader.ReadAll()
if err != nil {
log.Fatalf("Error reading csv file: %v", err)
}
correctCount := 0
fmt.Printf("Press Enter to start the quiz (and the timer of %d seconds)...\n", *timeLimitProvided)
fmt.Scanln()
workDone := make(chan bool)
timer := time.NewTimer(time.Duration(*timeLimitProvided) * time.Second)
go func() {
for _, problem := range data {
question := problem[0]
answer := problem[1]
fmt.Printf("%s = ", question)
var userAnswer string
fmt.Scanln(&userAnswer)
userAnswer = strings.TrimSpace(userAnswer)
if userAnswer == answer {
correctCount++
}
}
workDone <- true
}()
select {
case <-timer.C:
fmt.Println("TIME'S UP!")
fmt.Printf("\nYou scored %d out of %d\n", correctCount, len(data))
case <-workDone:
fmt.Printf("\nYou scored %d out of %d\n", correctCount, len(data))
}
}

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
