E Tech.

What I learned from creating Go CLI tool

Introduction

I use multiple Google Chrome profiles to separate use cases, and this works well for me. However, a few days ago, I had trouble remembering which profile I had saved an article in that I wanted to revisit.
Since I'm also interested in building CLI tools with Go, I decided to create a CLI tool that allows users to search bookmarks across all Chrome profiles using a keyword.

What I Made

I created this one(GitHub link).
Image for tool

Key Points I learned

Bubbletea

At first, I considered adding a richer UI, but the features I needed didn’t require a complex interface. I chose the Bubbletea library, which uses the Elm Architecture and it looked simple to study.
It’s easy to use and comes with many ready-made components. Bubbletea includes other libraries as well, such as the Bubbles library, which provides features like progress bars, tables, spinners, and more. Lipgloss also offers many styling options, including colors and width.

Testing in Go

I'm new to Go and didn’t know much about testing. Go has a standard testing library, and although you can choose other testing frameworks, I used the standard library in a simple way—just calling functions and comparing the expected and actual results.
The debate over whether to use external testing libraries versus the standard one is likely ongoing, as it depends on balancing dependency management and the amount of code. Next time, I plan to try out a different testing framework.

Read JSON Files Recursively

The bookmark file Chrome uses contains recursive structure to represent folders. It needs to define struct in this way:

type Child struct {
	Children     []Child   `json:"children"`
	DateAdded    string    `json:"date_added"`
	DateLastUsed string    `json:"date_last_used"`
	Guid         string    `json:"guid"`
	Id           string    `json:"id"`
	MetaInfo     Meta_info `json:"meta_info"`
	Name         string    `json:"name"`
	Type         string    `json:"type"`
	Url          string    `json:"url"`
}

The part Children []Child json:"children" is it. I'll check the difference []Child and []*Child in this case later.

Modules and Packages in Go

This was the most difficult part for me to understand. There are some ways to construct Go project.
Organizing a Go module
Call your code from another module
In my understanding, package is a group of files. Module is a kind of program. Package should have dedicated folder. Module needs go.mod file but package doesn't need.

Conclusion

Though this is very simple tool, I learned a lot of things I didn't know.
Of course there are many Chrome extensions to search bookmark. This might be reinventing the wheel... but it was a great learning experience!