Skip to content

Instantly share code, notes, and snippets.

@adilw3nomad
Last active June 22, 2019 18:41
Show Gist options
  • Save adilw3nomad/291659ce6b5896282faace0c8c34da1b to your computer and use it in GitHub Desktop.
Save adilw3nomad/291659ce6b5896282faace0c8c34da1b to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"encoding/csv"
"fmt"
"log"
"os"
)
func main() {
csvFile, err := os.Open("problems.csv")
if err != nil {
log.Fatal(err)
}
defer csvFile.Close()
reader := csv.NewReader(csvFile)
records, err := reader.ReadAll()
if err != nil {
log.Fatal(err)
}
for i := range records {
// Print the question out.
fmt.Println("Question: ", records[i][0])
// Read from stdin for answer.
inputReader := bufio.NewReader(os.Stdin)
fmt.Print("Enter your answer now: ")
// Expect an answer to be given once they hit return.
text, err := inputReader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
// Compare the answer given by the user to the correct answer
// Print a response accordingly.
if text == records[i][1] {
fmt.Println("Correct!")
} else {
fmt.Println("WRONG! Answer is: ", records[i][1])
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment