A small Go package for extracting and validating integer IDs from HTTP requests, designed for use with the chi/v5 router, but can be adapted for other frameworks.
- Extracts integer IDs from HTTP requests (URL params or form values)
- Supports int16, int32, and int64 types
- Customizable key and source via options
- Clear error handling for invalid or missing IDs
go get github.com/kaatinga/chid
import (
"net/http"
"github.com/kaatinga/chid"
)
// Example handler using chi router
func handler(w http.ResponseWriter, r *http.Request) {
id, err := chid.GetID[int64](r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Use id (int64)
}You can customize how the ID is extracted using options:
chid.WithIDKey(key string): Use a custom key instead of the default "id"chid.WithFormOrQuery(): Extract from form or query values instead of URL params
Example:
id, err := chid.GetID[int32](r, chid.WithIDKey("user_id"), chid.WithFormOrQuery())GetID returns descriptive errors for:
- Unsupported ID type
- Unable to parse the ID value
- ID is zero or below zero
MIT