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:

  1. Ensure you have Go installed on your system. You can download it from the official Go website: https://golang.org/dl/
  2. Create a new file named flashcard.go and copy the provided code into this file.
  3. Open your terminal or command prompt, navigate to the directory where flashcard.go is saved.
  4. Run the following command to execute the program:
    go run flashcard.go
  5. Follow the on-screen prompts to create and review flashcards.

 

© 2024 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)