Command-Line To-Do Application
Start with Command-Line To-Do Application
Learn command-line to-do application through an executable example, then connect the small demonstration to production code.
What you’ll learn
- Explain the main purpose of command-line to-do application.
- Read and run the complete Go example in this lesson.
- Apply the concept and recognize common mistakes.
Understand the idea
A useful CLI maps commands such as add, list and done to clear operations. Parse flags and arguments, validate errors and keep the task store independent of terminal I/O.
The starter exercises add and complete actions with fixed inputs; a real CLI reads os.Args and stores tasks across executions.
Visualize how it works
Follow this diagram before executing the example. Check where a value or decision changes what runs next.
Tools, syntax and practical notes
CLI scope: implement todo add "Learn Go", todo list and todo done 1. Add a configurable data location, helpful exit statuses, stable IDs and tests for malformed commands.
Run a complete Go example
This example is a complete program. Save it as main.go or paste it into the Go Playground.
Go code
package main
import "fmt"
type Todo struct {
Title string
Done bool
}
func main() {
tasks := []Todo{}
tasks = append(tasks, Todo{Title: "Learn Go"})
tasks = append(tasks, Todo{Title: "Write CLI"})
tasks[0].Done = true
for i, task := range tasks {
status := "open"
if task.Done {
status = "done"
}
fmt.Printf("%d. %s [%s]\n", i+1, task.Title, status)
}
}
Output
1. Learn Go [done]
2. Write CLI [open]Trace what the program does
Two tasks are appended; the first task’s Done field becomes true. The loop prints both tasks in insertion order with their current statuses.
Avoid common mistakes
- Keep identifiers stable if tasks may be sorted, edited or deleted.
- Do not parse untrusted command arguments by indexing without length checks.
Try it yourself
Implement add, list, done and delete commands with flag parsing and JSON storage.
Tip: Edit the example above, predict the output, and run the modified code to test your reasoning.
Check your understanding
What you should remember
You have practiced Command-Line To-Do Application in a small, self-contained program. The important habit is to predict what runs, inspect the actual output and distinguish the language rule from the particular example.
Before continuing, make sure you can explain the program to someone who has not seen it, including the line that changes its result.
Reaching the recap and completing the quizzes are separate milestones.