- Golang Nugget
- Posts
- Golang Nugget - January 21, 2025
Golang Nugget - January 21, 2025
Go might soon get a new way to handle err != nil, making your code cleaner and cutting down on boilerplate.
Hey Gophers,
I've got some fresh material this week to keep your Go skills on point – let’s jump into the latest nuggets! And if you find it helpful, don’t forget to share it with your friends and colleagues. After all, there’s nothing better than a friend who shares nuggets!
How HTTP/2 Works and How to Enable It in Go — HTTP/2 is a major upgrade over HTTP/1.1. HTTP/1.1 introduced pipelining, but requests had to be sent in order, and responses had to return in the same order. If one response was delayed, everything else in the queue had to wait—a problem known as Head-of-Line (HoL) blocking.
HTTP/2 solves this by splitting a single connection into multiple streams. Whether you realise it or not, you might already be using it in your Go applications. This is an in-depth article about HTTP/2 and how to ensure you're making the most of it in Go.
Leveraging Go’s Iterator Pattern for Unified Data Source Integration — Go 1.23 introduced the
iter
package to the standard library, providing a standardised way to handle iterations. The package introduces two main types of sequences:Seq
for single-value sequences andSeq2
for key-value pairs.An Interactive Tour of Go 1.24 — Go 1.24 is coming out, and it’s a good time to see what we should expect from it. Anton wrote one of his amazing interactive tours again, where he listed all the features with tons of runnable examples to demonstrate how to use them. It covers exciting updates like weak pointers, finalisers, and Swiss-map implementation for better performance.
Go Interfaces: Why, How and When — Interfaces allow us to reason about the higher-level logic of our processes without getting down to the small details. But that’s a double-edged sword, particularly in Go, as interfaces have memory implications. They introduce more abstraction to our code, which can make it susceptible to unnecessary wrappers, misuse of definitions, and sometimes even memory issues.
Tool Time
github.com/pingcap/tidb — TiDB is an open-source, cloud-native, distributed SQL database. It’s designed to provide the scalability of NoSQL databases while maintaining the ACID compliance and strong consistency of traditional relational databases. TiDB is MySQL 8.0 compatible and can be a drop-in replacement for it.
github.com/knadh/koanf — koanf is a library for reading configuration from different sources in different formats in Go applications. It is a cleaner, lighter alternative to spf13/viper with better abstractions and extensibility and far fewer dependencies.
github.com/cilium/ebpf — ebpf-go is a pure-Go library to read, modify and load eBPF programs and attach them to various hooks in the Linux kernel. If you’re new to eBPF, the following article is a very good intro.
Deep Dive
Like it or not, people are not happy with boilerplate error handling in Go. The H1 2024 survey pointed this out, and now there's another proposal for a new syntax to simplify error handling in Go.
The suggestion comes from Ian Taylor, who works on the Google Go team, and there's a lot of discussion under the issue, making it hard to ignore.
The core idea is to use a question mark (?
) to absorb error results and streamline error handling. It allows for statements like this:
r, err := SomeFunction()
if err != nil {
return fmt.Errorf("something failed: %v", err)
}
to be written as:
r := SomeFunction() ? {
return fmt.Errorf("something failed: %v", err)
}
Here, the ?
captures the error if it's not nil and executes the block of code that follows.
Similarly, this:
if err := SomeFunction2(); err != nil {
return err
}
can be simplified to:
SomeFunction2() ?
GopherCon 2021: Queues, Fairness, and The Go Scheduler - Madhav Jivrajani
I know, it’s January 2025, and I’m sharing a video from 2021! What’s wrong with me?
But this is a must-watch video if you’re interested in how Go handles scheduling and what that means for your applications. Unlike most other languages, Go has what we call green threads (goroutines), which are lightweight and scheduled by the Go runtime.
The problem? How to make it fair so each goroutine gets a chunk of CPU time and avoids starvation.
Go has gone through a few pivots in how to schedule goroutines and ensure fairness. This video goes through the history, talks about processors (logical units in the Go runtime) and processor queues, and explains in detail how Go makes sure each goroutine gets its fair share.
An entirely new way to present ideas
Gamma’s AI creates beautiful presentations, websites, and more. No design or coding skills required. Try it free today.
Reply