go-middleware

Implement Go HTTP middleware for context propagation, slog logging, and panic recovery.

Updated Feb 17, 2026
One-click install
npx skills add https://github.com/javierhbr/random-poc --skill go-middleware
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: go-middleware
Source: https://github.com/javierhbr/random-poc/tree/main/custom-skills/beagle-main/plugins/beagle-go/skills/go-middleware
Command: npx skills add https://github.com/javierhbr/random-poc --skill go-middleware

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill provides robust, idiomatic Go HTTP middleware patterns to handle cross-cutting concerns like context propagation, structured logging, error handling, and panic recovery, ensuring cleaner and more resilient Go web services.

Core Features & Use Cases

  • Context Propagation: Manages request-scoped data (request IDs, user info) using type-safe context keys.
  • Structured Logging: Integrates slog for consistent, searchable logs with customizable levels and sources.
  • Centralized Error Handling: Implements an AppHandler pattern for consistent error responses and uses a Recovery middleware to prevent panics from crashing the server.
  • Use Case: When building a new Go API, use this Skill's middleware to automatically add request IDs, log all incoming requests with relevant details, handle errors gracefully, and ensure the server remains stable even if a handler panics.

Quick Start

Apply the Recovery, RequestID, and Logger middleware to your HTTP router in the correct order.

Frequently Asked Questions about go-middleware

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I structure HTTP middleware in Go for context propagation and logging?

Use type-safe context keys to manage request-scoped data like request IDs and user info within your Go HTTP middleware. This prevents key collisions and ensures data propagates cleanly across the handler chain without type assertion errors.

What is the best way to handle errors centrally in a Go web service?

Implement an AppHandler pattern to centralize error handling in Go web services. This pattern wraps HTTP handlers to intercept domain errors and generate consistent HTTP error responses, keeping business logic clean and maintainable.

How do I prevent panics from crashing my Go HTTP server?

Apply a Recovery middleware to your HTTP router to catch handler panics and prevent server crashes. This middleware intercepts panics, logs the failure, and returns an HTTP 500 response, keeping the server stable.

How do I use slog for structured logging in Go HTTP middleware?

Integrate slog within a logging middleware that wraps HTTP handlers to capture request details. This provides consistent, searchable logs with customizable levels and source information directly tied to the request lifecycle.

In what order should I apply Recovery, RequestID, and Logger middleware in Go?

Apply Recovery, RequestID, and Logger middleware in that exact sequence. Recovery must wrap all handlers to catch panics, RequestID injects context data, and Logger records the request details using the generated request ID.

Does standard Go net/http support custom middleware for context and recovery?

Standard Go net/http supports custom middleware for context and recovery through function wrapping. You can chain http.Handler wrappers to inject context values, apply slog logging, and recover from panics without requiring third-party routing dependencies.