Introduction
A to-do list application is a fundamental project that helps in managing tasks efficiently. This application allows users to add, view, mark, and delete tasks, making it a great way to learn and practice Go programming concepts such as slices, functions, and user input handling.
Objective
The objective of this project is to build a simple console-based to-do list application where users can manage their tasks dynamically. This project demonstrates the use of Go’s data structures and standard library functions for practical programming.
Code
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
type Task struct {
ID int
Description string
Completed bool
}
var tasks []Task
var nextID = 1
func main() {
scanner := bufio.NewScanner(os.Stdin)
for {
fmt.Println("\nSimple To-Do List Application")
fmt.Println("1. Add Task")
fmt.Println("2. View Tasks")
fmt.Println("3. Mark Task as Completed")
fmt.Println("4. Delete Task")
fmt.Println("5. Exit")
fmt.Print("Choose an option: ")
scanner.Scan()
choice := scanner.Text()
switch choice {
case "1":
addTask(scanner)
case "2":
viewTasks()
case "3":
markTaskCompleted(scanner)
case "4":
deleteTask(scanner)
case "5":
fmt.Println("Exiting the application. Goodbye!")
return
default:
fmt.Println("Invalid option. Please try again.")
}
}
}
func addTask(scanner *bufio.Scanner) {
fmt.Print("Enter the task description: ")
scanner.Scan()
description := scanner.Text()
newTask := Task{
ID: nextID,
Description: description,
Completed: false,
}
tasks = append(tasks, newTask)
nextID++
fmt.Println("Task added successfully!")
}
func viewTasks() {
if len(tasks) == 0 {
fmt.Println("No tasks found.")
return
}
fmt.Println("\nTo-Do List:")
for _, task := range tasks {
status := "Incomplete"
if task.Completed {
status = "Completed"
}
fmt.Printf("%d. %s [%s]\n", task.ID, task.Description, status)
}
}
func markTaskCompleted(scanner *bufio.Scanner) {
fmt.Print("Enter the task ID to mark as completed: ")
scanner.Scan()
id, err := strconv.Atoi(scanner.Text())
if err != nil {
fmt.Println("Invalid input. Please enter a valid task ID.")
return
}
for i, task := range tasks {
if task.ID == id {
tasks[i].Completed = true
fmt.Println("Task marked as completed!")
return
}
}
fmt.Println("Task not found.")
}
func deleteTask(scanner *bufio.Scanner) {
fmt.Print("Enter the task ID to delete: ")
scanner.Scan()
id, err := strconv.Atoi(scanner.Text())
if err != nil {
fmt.Println("Invalid input. Please enter a valid task ID.")
return
}
for i, task := range tasks {
if task.ID == id {
tasks = append(tasks[:i], tasks[i+1:]...)
fmt.Println("Task deleted successfully!")
return
}
}
fmt.Println("Task not found.")
}
Explanation
The program structure is as follows:
- Task Struct: The
Task
struct represents a task with fields for an ID, description, and completion status. - Main Function: The main function presents a menu-driven interface for users to interact with the application.
- Task Management Functions:
addTask
: Adds a new task to the list.viewTasks
: Displays all tasks with their statuses.markTaskCompleted
: Marks a task as completed based on its ID.deleteTask
: Deletes a task from the list based on its ID.
How to Run the Program
- Ensure you have Go installed on your system. You can download it from
Go’s official website. - Save the code in a file named
todo_list.go
. - Open a terminal and navigate to the directory containing the file.
- Run the program with the following command:
go run todo_list.go
- Follow the on-screen instructions to manage your tasks.
Copyright © Learn Programming. All rights reserved.