Post

Building a CLI Tool in Go: A Simple Todo List Manager

Building a CLI Tool in Go: A Simple Todo List Manager

Introduction

Command-line tools are a staple in a developer’s workflow. Go is exceptionally well-suited for building fast, portable, and reliable CLI tools. In this post, we’ll build a basic Todo List Manager that allows you to add and list tasks directly from your terminal.

The CLI Tool Implementation

We will use the os package to handle command-line arguments and a simple text file for storage.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main

import (
    "fmt"
    "os"
)

func main() {
    if len(os.Args) < 2 {
        fmt.Println("Usage: todo [add|list] [task]")
        return
    }

    command := os.Args[1]

    switch command {
    case "add":
        if len(os.Args) < 3 {
            fmt.Println("Please provide a task description.")
            return
        }
        addTask(os.Args[2])
    case "list":
        listTasks()
    default:
        fmt.Println("Unknown command:", command)
    }
}

func addTask(task string) {
    f, _ := os.OpenFile("tasks.txt", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
    defer f.Close()
    f.WriteString(task + "\n")
    fmt.Println("Task added.")
}

func listTasks() {
    data, _ := os.ReadFile("tasks.txt")
    fmt.Print(string(data))
}

How to use it

After compiling your code with go build -o todo main.go, you can use it in your terminal:

1
2
./todo add "Learn Go CLI"
./todo list

Why Go for CLI?

  • Single Executable: Go compiles your code into a single binary, making it easy to distribute without dependencies.
  • Fast Startup: Go programs start almost instantaneously, which is critical for a smooth CLI experience.
  • Rich Standard Library: Packages like os, flag, and fmt provide everything needed to interact with the system, parse flags, and output text.

Conclusion

Building your own CLI tools in Go is a rewarding experience. It not only helps you automate your daily tasks but also deepens your understanding of system-level programming in Go.

Suggested Reading

This post is licensed under CC BY 4.0 by the author.