Documentation
binder maps the parts of an *http.Request onto the fields of a
struct, using struct tags to say where each field comes from, then runs
the validation your type defines. That is the whole library. It ships no
rule language, and does not route, log or transform — those belong to
packages that already do them well.
These pages document v1.2.0. The module path is
uradical.io/go/binder and it requires Go 1.27.
The shape of it
One call, one struct. Each field names a source and a key; binder reads the request once, converts each value to the field's type, and reports every field that could not be filled, together.
type UpdateUser struct {
ID int `path:"id"`
Fields []string `query:"fields"`
Name string `body:"name,required"`
Email string `body:"email,omitempty"`
Token string `cookie:"session"`
TraceID string `header:"X-Request-ID"`
}
func handler(w http.ResponseWriter, r *http.Request) {
var req UpdateUser
if err := binder.Bind(r, &req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// req is populated; every field came from where its tag says.
}Start here
What binder does not do
Knowing the edges early saves an afternoon of looking for a feature that was never there:
- No streamed uploads. A
multipart/form-databody binds its file parts, but the whole request is held in memory under the body size limit rather than spilled to disk. - No validation rules. Your
Validate(ctx)method runs as part of binding, but there are nomin,maxoremailtags. - No fixed-size arrays.
[]intbinds;[5]intdoes not. REST payloads are variable-length. - No defaults. A missing optional value leaves the field as it was, so set defaults on the struct before you bind.
- No routing.
path:reads whatnet/http's ownPathValuealready resolved.