Code Snippets

Language: Go

Wrap and Unwrap Errors in Go

Wrap errors with fmt.Errorf %w and inspect with errors.Is / As.

Author: chris

Connect to PostgreSQL in Go

Open a database/sql handle to Postgres and query rows.

Author: chris

Minimal HTTP Server in Go

Spin up a net/http server with a JSON-returning handler.

Author: chris

Parse Flags with the flag Package

Parse typed CLI flags using the stdlib flag package.

Author: chris

Synchronize with WaitGroup

Wait for a batch of goroutines using sync.WaitGroup.

Author: chris

Sort Slice of Structs in Go

Sort a slice of structs with sort.Slice and a less func.

Author: chris

Regex FindAllString in Go

Compile a regex and pull every match out of a string.

Author: chris

SHA256 Hash in Go

Compute a SHA256 digest with the crypto/sha256 stdlib.

Author: chris