sandgardenhq/sgai/go-http-server-routes

Claude Code and agent skill by sandgardenhq/sgai

Trust tier 2
  • community
LLM review pending
Source check pending

Checking that the SKILL.md file is still in the repository.

Go 1.22+ enhanced HTTP routing with method matching and wildcards. When defining HTTP routes using net/http ServeMux in Go 1.22+

Install

npx vskill@latest install sandgardenhq/sgai/go-http-server-routes

SKILL.md

Go HTTP Server Routes (Go 1.22+)

Enhanced routing patterns for net/http.ServeMux introduced in Go 1.22.

Reference URLs

  • https://go.dev/blog/routing-enhancements - Official announcement
  • https://pkg.go.dev/net/http#ServeMux - ServeMux documentation

Method Matching

Specify HTTP methods directly in patterns:

go
mux := http.NewServeMux()
mux.HandleFunc("GET /posts/{id}", getPost)
mux.HandleFunc("POST /posts", createPost)
mux.HandleFunc("PUT /posts/{id}", updatePost)
mux.HandleFunc("DELETE /posts/{id}", deletePost)
  • GET also matches HEAD requests
  • All other methods match exactly
  • Unmatched methods return 405 Method Not Allowed with Allow header

Wildcards

Single Segment: {name}

Matches exactly one path segment:

go
mux.HandleFunc("GET /users/{id}", func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
fmt.Fprintf(w, "User ID: %s", id)
})
Remaining Segments: {name...}

Matches all remaining path segments:

go
mux.HandleFunc("GET /files/{pathname...}", func(w http.ResponseWriter, r *http.Request) {
pathname := r.PathValue("pathname")
// /files/docs/readme.txt -> pathname = "docs/readme.txt"
})
Exact Trailing Slash: {$}

Matches only the path with trailing slash:

go
mux.HandleFunc("GET /posts/{$}", listPosts) // matches /posts/ only
mux.HandleFunc("GET /posts/{id}", getPost) // matches /posts/123
PatternMatchesDoes Not Match
/posts/{$}/posts//posts, /posts/123
/posts//posts/, /posts/123, /posts/a/b/posts
/posts/{id}/posts/123/posts/, /posts/a/b

PathValue API

Extract wildcard values from requests:

go
func getPost(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
// use id...
}

For third-party routers to integrate:

go
r.SetPathValue("id", "123")

Precedence Rules

The most specific pattern wins. A pattern is more specific if it matches a strict subset of requests.

Examples
Pattern APattern BWinnerReason
/posts/latest/posts/{id}ALiteral matches fewer requests
GET /posts/{id}/posts/{id}AMethod constraint is more specific
/users/{u}/posts/latest/users/{u}/posts/{id}ALiteral segment more specific
Conflicts

Two patterns conflict if neither is more specific than the other. Registering conflicting patterns causes a panic.

go
// CONFLICT - will panic
mux.HandleFunc("/posts/{id}", handler1)
mux.HandleFunc("/{resource}/latest", handler2)
// Both match /posts/latest, neither is more specific
Host Precedence

Patterns with hosts take precedence over patterns without:

go
mux.HandleFunc("example.com/", hostHandler) // wins for example.com
mux.HandleFunc("/", defaultHandler) // wins for other hosts

Complete Example

go
package main
import (
"encoding/json"
"net/http"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("GET /api/posts", listPosts)
mux.HandleFunc("GET /api/posts/{id}", getPost)
mux.HandleFunc("POST /api/posts", createPost)
mux.HandleFunc("PUT /api/posts/{id}", updatePost)
mux.HandleFunc("DELETE /api/posts/{id}", deletePost)
mux.HandleFunc("GET /static/{filepath...}", serveStatic)
http.ListenAndServe(":8080", mux)
}
func getPost(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
post, err := findPost(id)
if err != nil {
http.Error(w, "not found", http.StatusNotFound)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(post)
}
func serveStatic(w http.ResponseWriter, r *http.Request) {
filepath := r.PathValue("filepath")
http.ServeFile(w, r, "static/"+filepath)
}

Common Patterns

Resource CRUD
go
mux.HandleFunc("GET /api/{resource}", listHandler)
mux.HandleFunc("GET /api/{resource}/{id}", getHandler)
mux.HandleFunc("POST /api/{resource}", createHandler)
mux.HandleFunc("PUT /api/{resource}/{id}", updateHandler)
mux.HandleFunc("DELETE /api/{resource}/{id}", deleteHandler)
Nested Resources
go
mux.HandleFunc("GET /users/{userID}/posts", listUserPosts)
mux.HandleFunc("GET /users/{userID}/posts/{postID}", getUserPost)
Special Routes Before Wildcards
go
mux.HandleFunc("GET /posts/latest", getLatestPost) // more specific
mux.HandleFunc("GET /posts/{id}", getPost) // less specific

Compatibility

For code that relied on pre-1.22 behavior (literal braces in patterns):

bash
GODEBUG=httpmuxgo121=1 ./myapp

Common Mistakes

  • Forgetting method prefix - /posts/{id} matches all methods
  • Conflicting patterns - causes panic at registration, not runtime
  • Missing `{$}` - /posts/ matches /posts/anything, use /posts/{$} for exact match
  • Wildcard naming - {id} and {identifier} are different wildcards, name doesn't affect precedence

Security

  • Security scanPassed, 100 of 100Pattern checks on the skill's files.
  • Model reviewNot runA model review is scheduled for the next rescan.

Quality evaluation

  • Task evaluationNot runNo evaluation yet. Run one in Skill Studio.

Source

Popularity

Repository stars
113
Repository forks
6
7-day trend score
24

Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 27 Mar 2026, 7 months ago.

Works with

Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.

  • Claude Code
  • Cursor
  • GitHub Copilot
  • Windsurf
  • Codex
  • Gemini CLI
  • Cline
  • Amp
  • Roo Code
  • Goose
  • Continue
  • Kilo Code
  • Junie
  • Trae
  • OpenHands
  • Qwen Code
  • AI Agent
  • Tabnine Chat
  • Sourcegraph Cody
  • Amazon Q
  • Firebase Genkit
  • Sweep
  • Devin
  • Phind
  • Mentat
  • Claude Desktop
  • Aider
  • GPT Pilot
  • Plandex
  • bolt.new
  • v0
  • Replit Agent
  • Anse
  • TypingMind
  • BetterChatGPT
  • LibreChat
  • Warp AI
  • Pieces
  • Fig AI
Embed badge
vskill VERIFIED badge
[![vskill VERIFIED](https://verified-skill.com/api/v1/skills/sandgardenhq/sgai/go-http-server-routes/badge)](https://verified-skill.com/skills/sandgardenhq/sgai/go-http-server-routes)

Something wrong with this skill or its page? Tell us.