Introduction: A flashcard app is a great tool for active recall, a technique proven to help with learning and memorization. This app allows users to create and review flashcards, helping them study efficiently. It provides a simple, user-friendly interface for managing flashcards and testing knowledge on various topics.
Objective: The objective of this program is to create a flashcard app in Go that allows users to input flashcards with a question and an answer, and later review the flashcards to test their knowledge. It will work by storing flashcards in memory and presenting them to the user for review.
Go Flashcard App Code
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
// Flashcard struct stores the question and answer for each flashcard
type Flashcard struct {
Question string
Answer string
}
// Function to create a new flashcard
func createFlashcard() Flashcard {
scanner := bufio.NewScanner(os.Stdin)
fmt.Println("Enter the question:")
scanner.Scan()
question := scanner.Text()
fmt.Println("Enter the answer:")
scanner.Scan()
answer := scanner.Text()
return Flashcard{Question: question, Answer: answer}
}
// Function to review flashcards
func reviewFlashcards(flashcards []Flashcard) {
scanner := bufio.NewScanner(os.Stdin)
for _, card := range flashcards {
fmt.Println("\nQuestion: ", card.Question)
fmt.Print("Press Enter to reveal the answer...")
scanner.Scan() // wait for user input to continue
fmt.Println("Answer: ", card.Answer)
}
}
func main() {
var flashcards []Flashcard
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Println("\nFlashcard App Menu:")
fmt.Println("1. Create a flashcard")
fmt.Println("2. Review flashcards")
fmt.Println("3. Exit")
fmt.Print("Choose an option: ")
scanner.Scan()
choice := scanner.Text()
switch choice {
case "1":
flashcard := createFlashcard()
flashcards = append(flashcards, flashcard)
fmt.Println("Flashcard created successfully!")
case "2":
if len(flashcards) > 0 {
reviewFlashcards(flashcards)
} else {
fmt.Println("No flashcards available. Please create some first.")
}
case "3":
fmt.Println("Exiting the app.")
return
default:
fmt.Println("Invalid option, please try again.")
}
}
}
Program Explanation
Program Structure:
- Flashcard struct: This struct is used to store the question and the corresponding answer of each flashcard.
- createFlashcard function: This function allows users to input a question and an answer, creating a new flashcard and returning it.
- reviewFlashcards function: This function presents the stored flashcards one by one. The user is prompted to press Enter to reveal the answer.
- main function: The main function provides a simple menu where the user can choose to create new flashcards, review existing flashcards, or exit the application. It manages user input and calls the appropriate functions.
How to Run the Program:
- Ensure you have Go installed on your system. You can download it from the official Go website: https://golang.org/dl/
- Create a new file named
flashcard.go
and copy the provided code into this file. - Open your terminal or command prompt, navigate to the directory where
flashcard.go
is saved. - Run the following command to execute the program:
go run flashcard.go
- Follow the on-screen prompts to create and review flashcards.