Story Details

  • Context should go away for Go 2 (2017)

    Posted: 2025-01-21 08:08:24

    The author argues that Go's context.Context is overused and often misused as a dumping ground for arbitrary values, leading to unclear dependencies and difficult-to-test code. Instead of propagating values through Context, they propose using explicit function parameters, promoting clearer code, better separation of concerns, and easier testability. They contend that using Context primarily for cancellation and timeouts, its intended purpose, would streamline code and improve its maintainability.

    Summary of Comments ( 126 )
    https://news.ycombinator.com/item?id=42777625

    HN commenters largely agree with the author's premise that context.Context in Go is overused and often misused for dependency injection or as a dumping ground for miscellaneous values. Several suggest that structured concurrency, improved error handling, and better language features for cancellation and deadlines could alleviate the need for context in many cases. Some argue that context is still useful for request-scoped values, especially in server contexts, and shouldn't be entirely removed. A few commenters express concern about the practicality of removing context given its widespread adoption and integration into the standard library. There is a strong desire for better alternatives, rather than simply discarding the existing mechanism without a replacement. Several commenters also mention the similarities between context overuse in Go and similar issues with dependency injection frameworks in other languages.