Create and organize your favorite recipes using a simple Go program. Easily store and retrieve recipes with a friendly interface.
Introduction
In this project, we are going to build a digital recipe book using the Go programming language. The purpose of this program is to allow users to add, view, and manage their recipes efficiently. The goal is to create an organized and simple way to store different recipes, making it easier to find and keep track of them.
Objective
The main objective of this project is to design a basic digital recipe book that allows the following operations:
- Add a new recipe with its title and description.
- View all stored recipes.
- Search for recipes by title.
- Store the data in memory during the runtime.
Go Program Code
package main import ( "fmt" "strings" ) type Recipe struct { Title string Description string } var recipeBook []Recipe func addRecipe(title, description string) { recipe := Recipe{Title: title, Description: description} recipeBook = append(recipeBook, recipe) fmt.Println("Recipe added successfully!") } func viewRecipes() { if len(recipeBook) == 0 { fmt.Println("No recipes found.") return } for _, recipe := range recipeBook { fmt.Printf("Title: %s\nDescription: %s\n\n", recipe.Title, recipe.Description) } } func searchRecipe(query string) { found := false for _, recipe := range recipeBook { if strings.Contains(strings.ToLower(recipe.Title), strings.ToLower(query)) { fmt.Printf("Title: %s\nDescription: %s\n\n", recipe.Title, recipe.Description) found = true } } if !found { fmt.Println("No recipes found matching the search query.") } } func main() { var choice int for { fmt.Println("Recipe Book Menu:") fmt.Println("1. Add a Recipe") fmt.Println("2. View Recipes") fmt.Println("3. Search Recipe") fmt.Println("4. Exit") fmt.Print("Enter your choice: ") fmt.Scan(&choice) switch choice { case 1: var title, description string fmt.Print("Enter recipe title: ") fmt.Scan(&title) fmt.Print("Enter recipe description: ") fmt.Scan(&description) addRecipe(title, description) case 2: viewRecipes() case 3: var query string fmt.Print("Enter search query: ") fmt.Scan(&query) searchRecipe(query) case 4: fmt.Println("Exiting Recipe Book. Goodbye!") return default: fmt.Println("Invalid choice. Please try again.") } } }
Explanation of Program Structure
The Go program is designed to manage a digital recipe book. Below is an overview of its key components:
- Recipe struct: This struct defines the structure of each recipe, containing a title and a description.
- Global recipeBook variable: This slice holds all the recipes added by the user during runtime.
- addRecipe function: This function adds a new recipe to the recipeBook slice.
- viewRecipes function: This function displays all the recipes stored in the recipeBook.
- searchRecipe function: This function searches for recipes by title based on user input.
- main function: The main function contains a simple menu system allowing the user to interact with the program, including adding a recipe, viewing all recipes, and searching for a recipe.
How to Run the Program
-
- Install Go: Download and install the Go programming language from here.
- Write the program: Copy the Go code provided into a text file and save it with a “.go” extension (e.g., “recipe_book.go”).
- Run the program: Open a terminal, navigate to the directory containing the file, and execute the following command:
go run recipe_book.go
- The program will display a menu with options. You can add recipes, view recipes, search for recipes, and exit the program.