How to Test Goroutines in Go

Goroutines are one of the most powerful features in Go. They allow us to run tasks concurrently and are also lightweight compared to threads. However, testing goroutines can be challenging because they execute in a random order. Sometimes, the test may finish before the goroutine completes, causing the test to fail intermittently. In this article, we’ll explore how to test goroutines in Go using just the standard library. Let’s consider this simple example:...

April 9, 2024 · 7 min · 1344 words · Jose Sitanggang

Graceful Shutdown in Go

Imagine you have a production service running on Kubernetes that is currently processing client requests while also deploying a new version to production. What will happen to those requests? Will those requests be lost, or will your service wait until all requests have been completed before upgrading? Let’s watch the demo below to provide a better context for the problem we are trying to solve. In the first part, the server is not implementing graceful shutdown....

November 2, 2023 · 7 min · 1486 words · Jose Sitanggang

Practical Design Pattern in Go: Functional Options

The Functional Options Pattern is a design pattern for creating objects with flexible configurations by using functions as arguments to modify the default behavior. In my opinion, this pattern can be considered a Builder Pattern with a functional style: instead of chaining methods, we compose functions to configure the object. This pattern is widely used in Go, which might be the reason it is not explained in most design pattern books....

October 30, 2023 · 7 min · 1428 words · Jose Sitanggang

Practical Design Pattern in Go: Adapter

The Adapter Pattern is a structural design pattern that allows objects with incompatible interfaces to collaborate1. Let me clarify something: an interface does not always mean the type Something interface, but in this context, it is more likely to refer to the contract between types. ATTENTION: I am not sponsored by Refactoring.Guru, but I definitely recommend that you buy the “DESIGN PATTERNS” book. It covers all known design patterns in depth and provides easy and simple explanations....

October 29, 2023 · 7 min · 1482 words · Jose Sitanggang

Namespace in Go

Actually, Go doesn’t have a namespace feature like C++ and C# does. However, we can achieve the same effect in Go. How? Let’s examine the problem for a moment to ensure that we are on the same page. I have a package called httpkit. This package contains many helpers for developing REST APIs. package httpkit // MuxOption is an option for customizing the ServeMux. type MuxOption func(mux *ServeMux) // NewServeMux creates a new ServeMux with given options....

October 29, 2023 · 6 min · 1239 words · Jose Sitanggang