The Powerful Interface in Go — Inspired by a Simple Todo CLI Tool

I am building an Todo CLI tool recently and used Go’s powerful interface to achieve better formatting. Previously my cli looks like: $ ./todo -list Another ToDo item Improve usage Improve output And the code looks like: l := &todo.List{} switch { case *list: for _, t := range *l { if !t.Done { fmt.Println(t.Task) } } But I don’t just want to print out the items I put inside the list, I want to items printed out with number and a mark “X” to indicate if the item was completed or not....

November 11, 2023 · 4 min · 812 words · David Lee

Demystify Unit Tests

There was a time I argue within my dev team what a unit test is. Many devs write unit tests with a real DB interaction or with other dependecies but they still call it unit tests. I strongly agree that when we want to test interactions, we should NOT mock the DB with 3rd party dependencies like “github.com/DATA-DOG/go-sqlmock”: When we want to test interactions, we should and always use real DB....

October 3, 2023 · 4 min · 840 words · David Lee

My Tricks on Outpacing an Entire Team in Tests

I don’t want to waste your time and let’s hit the point, this article mainly discusses about how to speed up your test experience with mocks. below are my tricks used in generate.go file: This is the package we need to install: go install github.com/vektra/mockery/v2@latest Mockery provides the ability to easily generate mocks for Golang interfaces. Running go generate will generate mocks for all interfaces within the directories: The above example shows that interfaces in application....

October 3, 2023 · 2 min · 421 words · David Lee

Nginx as Reverse Proxy in Microservices

Developers might find topics related to DevOps quite annoying, but once you master this skill, and you can be invincible. Just like we cannot skip Docker, let’s challenge Nginx heads on. Setting up Nginx as a reserve proxy might not be that troublesome as you expect. Check below the nginx.conf file: It’s quite easy to understand above code. The server listens on port 8080 and proxies requests to various upstream servers based on the URL path....

October 3, 2023 · 4 min · 728 words · David Lee

How to Manage Interfaces Gracefully in Event-Driven Architecture

This could be a large topic and there are many ways of handle interfaces gracefully, and here I’m going to use one simple example to explain this. Please see below code: I have interface defined in package domain: I have interface implementation embeded in package grpc: Package domain: This package defines an interface named StoreRepository. This interface specifies a set of methods that any concrete type (struct) must implement to satisfy the interface....

September 28, 2023 · 4 min · 675 words · David Lee