🪟 Windows TippsThe Gemini desktop app is now available for Windows(11.09.2026 um 17:06 Uhr)
🪟 Windows TippsWindows Authentication SMS not received or working(12.09.2026 um 11:54 Uhr)
⚠️ Malware / Trojaner / VirenWindows 11 just dropped the tool ransomware abused, Microsoft says don’t restore WMIC(10.09.2026 um 20:11 Uhr)
🕵️ SicherheitslückenDefender 0-Day ShieldBreak (CVE-2026-69414) nicht sauber gepatcht - BornCity(11.09.2026 um 12:52 Uhr)
🪟 Windows TippsServertimeout in Outlook über 10 Minuten verlängern(12.09.2026 um 15:10 Uhr)
🪟 Windows TippsThe Gemini desktop app is now available for Windows(11.09.2026 um 17:06 Uhr)
🪟 Windows TippsWindows Authentication SMS not received or working(12.09.2026 um 11:54 Uhr)
⚠️ Malware / Trojaner / VirenWindows 11 just dropped the tool ransomware abused, Microsoft says don’t restore WMIC(10.09.2026 um 20:11 Uhr)
🕵️ SicherheitslückenDefender 0-Day ShieldBreak (CVE-2026-69414) nicht sauber gepatcht - BornCity(11.09.2026 um 12:52 Uhr)
🪟 Windows TippsServertimeout in Outlook über 10 Minuten verlängern(12.09.2026 um 15:10 Uhr)

🔧 Programmierung 🕛 vor 1 Jahr 7 Min Lesezeit
0

Simple Go CLI-Todo App

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

Hey Guys!



I haven't blogged on Dev in over 2 years! It's been a while so please excuse me if my typing skills have degraded over time.



I'm currently learning Go and received a list of projects to complete and share, one of these happens to be a simple Golang cli-todo app that allows someone to add todos to a list of tasks and achieve a set of basic functionality on these tasks.



These include:



1) Listing Tasks

2) Adding More Tasks

3) Modifying These Tasks

4) Making Tasks Completed



Unfortunately, I don't have a fancy name for it 😅 as it's designed to be a lightweight, easy-to-use app that someone can complete in a day. Even if you're a beginner.







Let's Begin



Well as usual, the creation of our main.go. Once this is setup, we will need to define the structure and functionality of our todos. I did so in a separate todo.go




CODE
type Todo struct {
Title string
Completed bool
CreatedAt time.Time
CompletedAt *time.Time
}






with a slice to hold our todos




CODE
type Todos []Todo






Then we'll need the implementation of the main methods of functionality, which include:




  • add




CODE
func (todos *Todos) add(title string) {
todo := Todo{
Title: title,
Completed: false,
CompletedAt: nil,
CreatedAt: time.Now(),
}

*todos = append(*todos, todo)
}






Creates a Todo object with a title, sets its Completed status to false, and appends it to the Todos slice.




  • delete




CODE
func (todos *Todos) delete(index int) error {
t := *todos

if err := t.validateIndex(index); err != nil {
return err
}

*todos = append(t[:index], t[index+1:]...)

return nil
}






Validates the index, then uses slicing to remove the item from the Todos list.




  • toggle




CODE
func (todos *Todos) toggle(index int) error {
if err := todos.validateIndex(index); err != nil {
return err
}

t := *todos
todo := &t[index]

if !todo.Completed {
completedTime := time.Now()
todo.CompletedAt = &completedTime
} else {
todo.CompletedAt = nil
}

todo.Completed = !todo.Completed
return nil
}






Validates the index, flips the Completed boolean, and updates the CompletedAt timestamp accordingly.



The rest of the methods follow a very similar functionality, if any issues, feel free to check out the



It can be installed with:




CODE
go get github.com/aquasecurity/table






Then I made a method to display the todos using methods from external the package. Particularly SetRowLines, SetHeaders, New, AddRow & Render were the primarily used ones in my case.




CODE
func (todos *Todos) print() {
table := table.New(os.Stdout)
table.SetRowLines(false)
table.SetHeaders("#", "Title", "Completed", "Created At", "Completed At")

for index, t := range *todos {
completed := "❌"
completedAt := ""

if t.Completed {
completed = "✅"
if t.CompletedAt != nil {
completedAt = t.CompletedAt.Format(time.RFC1123) //time standard
}
}

table.AddRow(strconv.Itoa(index), t.Title, completed, t.CreatedAt.Format(time.RFC1123), completedAt)
}

table.Render()
}






The print method is a neat way to show the list of todos in the terminal. It creates a table with columns for things like the task number, title, whether it's completed, when it was created, and when it was completed.



It goes through each todo item, checks if it's done or not, and adds a ✅ if it's completed or a ❌ if it isn't. If the task is finished, it even shows the exact date and time it was completed.



Once all the rows are ready, it prints the table out in a clean, readable format. Super handy for quickly seeing the status of all a user's tasks at a glance!






How about saving these todos?



So I thought that the functionality of saving the todos locally to let's say a file, in this case, todos.json, and then reading from there would be a good idea. Essentially having some level of persistence of our data regarding each and all todos.



We could add this functionality to an existing file, but I think it's a good idea to separate concerns.



I added a storage.go, it could be called whatever you'd like store.go, persist.go, etc.



I chose JSON but the same principles usually apply to any data format you'd like to save the data too.




CODE
package main

import (
"encoding/json"
"os"
)

type Storage[T any] struct {
Filename string
}

func NewStorage[T any](filename string) *Storage[T] {
return &Storage[T]{Filename: filename}
}

func (s *Storage[T]) Save(data T) error {
fileData, err := json.MarshalIndent(data, "", " ")
if err != nil {
return err
}

return os.WriteFile(s.Filename, fileData, 0644) // file mode
}

func (s Storage[T]) Load(data *T) error {
fileData, err := os.ReadFile(s.Filename)
if err != nil {
return err
}

return json.Unmarshal(fileData, data) // convert and populate data
}







  • There's a Storage struct that keeps track of the file being worked with.


  • The NewStorage function helps set things up by just giving it the file name.


  • The Save method takes the data, turns it into pretty JSON, and writes it to the file (todos.json). If something goes wrong, it tells us with an error.


  • The Load method does the opposite—reads the file, unpacks the JSON, and fills the fileData with the data.




It’s an easy, reusable way to handle saving and loading any kind of data without needing a database or anything fancy.



From here I make use of the NewStorage in the main.go to add some todos to my list and save them which can now be viewed in my todos.json




CODE
func main() {
todos := Todos{}
// Load data (todos) from storage:
storage := NewStorage[Todos]("todos.json")
err := storage.Load(&todos)
if err != nil {
fmt.Println("Warning: Could not load todos from storage. Starting fresh todos.")
}
// Parse & Execute
cmdFlags := NewCmdFlags()
cmdFlags.Execute(&todos)
// Save to storage
err = storage.Save(todos)
if err != nil {
fmt.Printf("Error saving todos in storage: %v\n", err)
}
}












CODE
[
{
"Title": "Go for a drive",
"Completed": false,
"CreatedAt": "2024-11-26T15:26:06.691069-05:00",
"CompletedAt": null
},
{
"Title": "Eat dinner",
"Completed": true,
"CreatedAt": "2024-11-26T15:26:37.361324-05:00",
"CompletedAt": "2024-11-26T15:33:03.088908-05:00"
},
{
"Title": "Walk my dog",
"Completed": false,
"CreatedAt": "2024-11-27T17:44:04.963474-05:00",
"CompletedAt": null
},
{
"Title": "Go hiking later with ashley",
"Completed": false,
"CreatedAt": "2024-11-27T17:51:01.667686-05:00",
"CompletedAt": null
}
]






For the commands, I didn't make anything fancy. I defined the flags I'll use as a struct




CODE
type CmdFlags struct {
Add string
Del int
Edit string
Toggle int
List bool
}






then a simple function using the flag package the list these flags, give them more details & descriptions, and customize them. I've also heard good things about the

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
The Gemini desktop app is now available for Windows
1 Quelle
Windows Authentication SMS not received or working
1 Quelle
Windows 11 just dropped the tool ransomware abused, Microsoft says don’t restore WMIC
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Simple Go CLI-Todo App

Thematisch verwandte Begriffe: Simple, CLITodo · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...