The context API is a critical foundation for modern Go development.  This is especially the case with APIs that use concurrency under the hood or perf

matttproud.com (blog)

submited by
Style Pass
2024-02-12 14:30:03

The context API is a critical foundation for modern Go development. This is especially the case with APIs that use concurrency under the hood or perform distributed operations. Given this, I would like to do a deep dive on several topics around the context API. I expect this post to be the first of several. In this one, I cover contexts and program scopes.

Program scope and contexts sound like a mundane topic, but a fair bit of confusion arises with contexts in industry. The most recent example I saw was this post on Reddit: Should contexts be used for long-running operations?

So why is this even a question at all? It’s one frequently asked; I see it raised in code review more than I am comfortable admitting. The answer lies in a combination of history and technical documentation.1

The first time the necessity for something similar to the context API was hinted at was in a March 2014 blog post entitled: Go Concurrency Patterns: Pipelines and cancellation. It teased how something like context API could be used to make distributed and concurrent operations behave synchronously, which is foundational in good API design in Go.

Leave a Comment