0 day streak
Code library

Intermediate Go exercises

Read these Intermediate Go snippets line by line — each one comes with a written breakdown of what the code does and why.

Go · Intermediate

Implicit Interface Implementation

In Go, interfaces are implemented implicitly. A type satisfies an interface by simply implementing its methods; there is no 'implements' keyword. This decouples the definition of the interface from…

Design PatternsBest Practices
Open snippet →
Go · Intermediate

Custom Function Types for Strategy Pattern

Functions in Go are first-class citizens. You can define custom function types, which is useful for implementing patterns like Strategy or Middleware, allowing behavior to be passed as arguments.

FunctionsDesign Patterns
Open snippet →
Go · Intermediate

Error Wrapping and Unwrapping

Go 1.13+ introduced error wrapping using the %w verb in fmt.Errorf. This allows you to add context to an error while still being able to check for the original sentinel error using errors.Is.

Error HandlingBest Practices
Open snippet →
Go · Intermediate

Non-Blocking Channel Operations with Select

The 'select' statement lets a goroutine wait on multiple communication operations. Using a 'default' case allows for non-blocking sends and receives, executing immediately if no channel is ready.

Async & ConcurrencyControl Flow
Open snippet →
Go · Intermediate

Composition via Struct Embedding

Go uses struct embedding to achieve composition. When a type is embedded without a field name, its methods and fields are 'promoted' to the outer struct, allowing them to be accessed directly.

SyntaxDesign Patterns
Open snippet →
Go · Intermediate

Synchronizing Tasks with sync.WaitGroup

A WaitGroup waits for a collection of goroutines to finish. The main goroutine calls Add to set the number of goroutines to wait for, each worker calls Done when finished, and Wait blocks until all…

Async & ConcurrencyFunctions
Open snippet →
Go · Intermediate

Implementing Table-Driven Tests

Table-driven testing is a Go idiom where test cases are defined as a slice of anonymous structs. This makes it easy to add new scenarios and ensures consistent testing logic across multiple inputs.

TestingBest Practices
Open snippet →
Go · Intermediate

Managing Cancellation with context.WithTimeout

The context package is essential for managing the lifecycle and cancellation of concurrent processes. WithTimeout creates a derived context that automatically cancels itself after a specified durat…

Async & ConcurrencyBest Practices
Open snippet →
Go · Intermediate

Handling UTF-8 Text Correctly with Runes

In Go, strings are read-only slices of bytes. UTF-8 characters like emojis or accented letters occupy multiple bytes. Slicing or indexing a string directly accesses raw bytes, which can corrupt mul…

Data TypesSyntax
Open snippet →
Go · Intermediate

Thread-Safe Map Access Using Read-Write Mutexes

In Go, maps are not safe for concurrent use. If two goroutines write to a map at the same time, or one reads while another writes, the program will panic with a fatal concurrent map write error. To…

Data TypesAsync & ConcurrencyBest Practices
Open snippet →
Go · Intermediate

Guaranteed One-Time Initialization via sync.Once

The sync.Once type guarantees that a function is executed exactly once across all goroutines. This is commonly used for thread-safe lazy initialization, such as setting up global configurations, co…

Async & ConcurrencyDesign PatternsBest Practices
Open snippet →