Create a Task List Manager in Go

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.

1- Define a Task struct with the following fields :
2- Create a TaskList struct that contains a slice of tasks.
3- Implement the following methods for TaskList :

Solution :

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()
}
            

Explanation of the Code :

FAQ :

Final Thoughts :

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! πŸ™Œ


β˜• Buy me a coffee