Introduction
Managing a shopping list is a common daily task, and creating an application for this can help users organize their purchases efficiently. This program demonstrates how to build a shopping list application using Go (Golang). The application allows users to add items, view the list, and delete items, showcasing basic Go programming concepts.
Objective
The objective of this program is to create a simple command-line shopping list application in Go. The application will:
- Add items to the shopping list
- Display the current shopping list
- Delete items from the list
This program serves as an introduction to Go programming for beginners and demonstrates key concepts like slices, loops, and user input handling.
Code
package main import ( "bufio" "fmt" "os" "strings" ) func main() { shoppingList := []string{} reader := bufio.NewReader(os.Stdin) for { fmt.Println("\nShopping List Application") fmt.Println("1. Add item") fmt.Println("2. View list") fmt.Println("3. Delete item") fmt.Println("4. Exit") fmt.Print("Enter your choice: ") choice, _ := reader.ReadString('\n') choice = strings.TrimSpace(choice) switch choice { case "1": fmt.Print("Enter item to add: ") item, _ := reader.ReadString('\n') item = strings.TrimSpace(item) shoppingList = append(shoppingList, item) fmt.Println("Item added successfully!") case "2": fmt.Println("\nCurrent Shopping List:") if len(shoppingList) == 0 { fmt.Println("The shopping list is empty.") } else { for i, item := range shoppingList { fmt.Printf("%d. %s\n", i+1, item) } } case "3": fmt.Println("\nCurrent Shopping List:") if len(shoppingList) == 0 { fmt.Println("The shopping list is empty.") break } for i, item := range shoppingList { fmt.Printf("%d. %s\n", i+1, item) } fmt.Print("Enter the number of the item to delete: ") var index int _, err := fmt.Scan(&index) if err != nil || index < 1 || index > len(shoppingList) { fmt.Println("Invalid input. Please try again.") } else { shoppingList = append(shoppingList[:index-1], shoppingList[index:]...) fmt.Println("Item deleted successfully!") } case "4": fmt.Println("Exiting the application. Goodbye!") return default: fmt.Println("Invalid choice. Please enter a valid option.") } } }
Explanation
The program structure is as follows:
- Initialize shopping list: A slice is used to store the shopping list items dynamically.
- Display menu options: A menu with four options is displayed in a loop until the user chooses to exit.
- Handle user input: User input is read using
bufio.NewReader
and processed based on their choice. - Add items: Items entered by the user are added to the slice.
- View list: The program iterates over the slice to display all items.
- Delete items: The user specifies the index of the item to delete, and the program updates the slice accordingly.
- Exit: The program terminates when the user selects the exit option.
How to Run
- Install Go from the official Go website.
- Copy the code into a file named
shopping_list.go
. - Run the program using the command:
go run shopping_list.go
- Follow the on-screen instructions to add, view, or delete items from the shopping list.