In this exercise, you will build a Task List Manager in Go. This will help you practice structs, methods, and slices while implementing key operations like adding, updating, removing, and displaying tasks.
fmt.Printf()
for structured output.Here is a corrected version of the Task List Manager in Go :
package main
import "fmt"
// Task struct represents a single task
type Task struct {
ID int
Title string
Description string
Status bool
}
// TaskList struct manages a collection of tasks
type TaskList struct {
Tasks []Task
}
// Display all tasks
func (tl *TaskList) ShowTasks() {
fmt.Println("Task List:")
for _, task := range tl.Tasks {
fmt.Printf("ID: %d | Title: %s | Description: %s | Status: %v\n",
task.ID, task.Title, task.Description, task.Status)
}
}
// Add a new task
func (tl *TaskList) AddTask(title, description string) {
newID := len(tl.Tasks) + 1
newTask := Task{
ID: newID,
Title: title,
Description: description,
Status: false,
}
tl.Tasks = append(tl.Tasks, newTask)
fmt.Println("Task successfully added:", newTask.Title)
}
// Remove a task by ID
func (tl *TaskList) RemoveTask(id int) {
indexToRemove := -1
for i, task := range tl.Tasks {
if task.ID == id {
indexToRemove = i
break
}
}
if indexToRemove == -1 {
fmt.Printf("No task found with ID %d.\n", id)
return
}
tl.Tasks = append(tl.Tasks[:indexToRemove], tl.Tasks[indexToRemove+1:]...)
fmt.Printf("Task with ID %d successfully removed.\n", id)
}
// Mark a task as completed
func (tl *TaskList) MarkAsDone(id int) {
for i, task := range tl.Tasks {
if task.ID == id {
tl.Tasks[i].Status = true
fmt.Printf("Task with ID %d marked as completed.\n", id)
return
}
}
fmt.Printf("No task found with ID %d.\n", id)
}
// Update a task's details
func (tl *TaskList) UpdateTask(id int, newTitle, newDescription string) {
for i, task := range tl.Tasks {
if task.ID == id {
tl.Tasks[i].Title = newTitle
tl.Tasks[i].Description = newDescription
fmt.Printf("Task with ID %d successfully updated.\n", id)
return
}
}
fmt.Printf("No task found with ID %d.\n", id)
}
// Main function to test the Task List Manager
func main() {
var taskList TaskList
// Adding tasks
taskList.AddTask("Learn Go", "Read the official documentation")
taskList.AddTask("Build a project", "Create a task manager")
// Display tasks before update
fmt.Println("\n Task list before update:")
taskList.ShowTasks()
// Update a task
taskList.UpdateTask(1, "Learn Go in depth", "Practice advanced exercises")
// Mark a task as done
taskList.MarkAsDone(1)
// Remove a task
taskList.RemoveTask(2)
// Display tasks after modifications
fmt.Println("\n Task list after modifications:")
taskList.ShowTasks()
}
Task struct
defines the structure of a task, including its ID, title, description, and completion status.TaskList
struct holds a slice of tasks and manages them.ShowTasks
method displays all tasks in a formatted way.AddTask
method creates and adds a new task with an automatically assigned ID.RemoveTask
method searches for a task by ID and removes it from the list.MarkAsDone
method updates the taskβs status to completed.UpdateTask
method allows modifying the title and description of an existing task.main()
function tests all these methods to verify the functionality. The program will display a message: β No task found with ID X and continue running.
Currently, no. However, you can extend the code by adding an UpdateStatus(id int, status bool)
function.
You can store tasks in a JSON file and load them when the program starts.
You can use thebufio
package to accept user input and interact with tasks in real-time.
This exercise helps you understand how to work with Go structs and methods in a real-world application. Try implementing additional features such as saving tasks to a file or creating a command-line interface for user input.
π Now it's your turn to experiment and improve this project!
π Thank you for reading these articles! If you find this content valuable and want to support my work, a coffee would be greatly appreciated! βπ
π» I am a freelance web developer, and I personally create and maintain this website. Any support would help me improve and expand it further! π