Home avatar

A collection of dev guides, tutorials and thoughts on various tech stacks, tools and programming languages.

The prototype pattern in Go

This is a continuation of the common design patterns I found in my old code series, which I started in a previous post.

While going through some of the code I found a couple of instances where I was making copies of some structs, but I wasn’t using the built in copy() method. I was, instead, using some custom copy logic.

The reason for that was that the struct had some properties that were slices of other structs and if I were to use copy(), it would get me in trouble as there was a possibility that the source struct could be mutated.

The factory method pattern in Go

This is a continuation of the common design patterns I found in my old code series, which I started in a previous post.

Another common pattern I found is the factory method pattern, which is a design pattern used to create different types of objects using the same interface.

This pattern is actually pretty common in Go. Some good example of this are the builtin I/O libraries:

package main

import (
  "bytes"
  "encoding/csv"
  "fmt"
  "io"
  "os"
)

func main() {
  f, _ := os.Open("avengers.csv")
  records, _ := parseCsv(f)
  fmt.Println("Records from file", records)
  
  data := []byte("name,surname\nJohn,Snow\n")
  r := bytes.NewReader(data)
  records, _ = parseCsv(r)
  fmt.Println("Records from bytes", records)
}

func parseCsv(r io.Reader) ([][]string, error) {
  cr := csv.NewReader(r)
  return cr.ReadAll()
}

Both the bytes.NewReader() and os.Open() implement the io.Reader interface which comes in handy for the parseCsv() method above as we can use it to parse data from multiple sources.

The singleton pattern in Go

I recently started going through some of my old code and I was trying to identify some common design patterns. I thought it could be a good memory exercise and refresher on software design patterns as it’s been quite some time since I last read through that.

And while I was doing that, I thought it might be a good idea to write about the patterns with the most occurrences.

Cross-compile gRPC for ARM with Docker

If you are building a microservice architecture, you will likely use some networking library to manage service communication.

This is where gRPC fits in. Interoperability across different languages is one of its strongest points, backed by extensive documentation and a large ecosystem.

Cross-compiling C++ applications that rely on gRPC for a different architecture (such as 32-bit ARM armhf or 64-bit ARM aarch64) is non-trivial because of code generation:

  1. Host tools: gRPC code generators (protoc and grpc_cpp_plugin) must execute natively on the build machine during compilation to produce C++ protobuf and gRPC stub files (hello.pb.cc, hello.grpc.pb.cc).
  2. Target libraries: The gRPC runtime libraries, Abseil, and SSL dependencies must be compiled with the target cross-compiler (arm-linux-gnueabihf-g++ or aarch64-linux-gnu-g++) and staged in a sysroot prefix.
  3. Application build: The application CMake build loads target libraries from the staging prefix while executing the host code generators to produce the final ARM executables.

The official gRPC repo includes a cross-compilation example. This guide modernizes that workflow using Docker Buildx, Debian Bookworm, and CMake, supporting both 32-bit and 64-bit ARM architectures.

Cross-compile Go with C dependencies for ARM

Go includes first-class support for cross-compiling pure Go code to multiple operating systems and architectures simply by specifying the $GOOS and $GOARCH environment variables (see the official environment list).

However, when your project uses C libraries or legacy system APIs via cgo, compilation requires a matching C cross-compiler and target C runtime headers. Configuring this directly on your host machine can quickly lead to conflicting toolchains and package versions.

Using Docker multi-stage builds and Buildx provides a reproducible, hermetic cross-compilation pipeline for both 64-bit ARM (arm64 / aarch64) and 32-bit ARM (armhf).

Build Docker images on k8s nodes

Docker Buildx allows you to route image build execution away from your local workstation using pluggable builder drivers:

  1. docker: Uses the standard daemon engine (default). Best for single-arch, local image generation.
  2. docker-container: Spawns an isolated BuildKit container locally. Supports multi-platform builds and cache export.
  3. kubernetes: Deploys BuildKit pods dynamically into a Kubernetes cluster, offloading compilation, packaging, and disk I/O to remote node pools.
  4. remote: Directly connects to a standalone BuildKit daemon over mTLS or TCP, useful for dedicated shared build infrastructure.

The kubernetes driver is particularly effective when local CPU and RAM are constrained, or when you want your build tasks to scale horizontally across high-performance cloud compute.

How to install and run QEMU on K8s nodes?

If you find yourself having to emulate different CPU architectures in a Kubernetes environment you’ll probably end up running some version of binfmt as an init container or maybe manually run it once.

That would probably be ok in most cases, but wouldn’t work if, for example, you’re running multiple pod replicas on the same node concurrently (say, when you setup a CI that spawns pods for every job in your pipeline) or when nodes are autoscaled.

How to access private Github repos when building Docker images?

If you need to access private GitHub repositories while building a Docker image without leaving SSH keys or tokens embedded in intermediate layers or the final image, BuildKit’s SSH mount feature (RUN --mount=type=ssh) provides a secure, leak-free solution.

In modern Docker (v23+), BuildKit is the default build engine and SSH mounts are fully stable; you no longer need to enable experimental flags for the CLI or the daemon.

Ensure your SSH agent is running and has your private key loaded: