Golang
Golang

 

 

 

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

  1. Ensure you have Go installed on your system. You can download it from
    Go’s official website.
  2. Save the code in a file named todo_list.go.
  3. Open a terminal and navigate to the directory containing the file.
  4. Run the program with the following command:
    go run todo_list.go
  5. Follow the on-screen instructions to manage your tasks.

Copyright © Learn Programming. All rights reserved.

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)