Installation
Install and set up the Rivaas binding package for your Go application
3 minute read
High-performance request data binding for Go web applications. Maps values from various sources (query parameters, form data, JSON bodies, headers, cookies, path parameters) into Go structs using struct tags.
Note: For validation (required fields, enum constraints, etc.), use the
rivaas.dev/validationpackage separately after binding.
import "rivaas.dev/binding"
type CreateUserRequest struct {
Name string `json:"name"`
Email string `json:"email"`
Age int `json:"age"`
}
// Generic API (preferred)
user, err := binding.JSON[CreateUserRequest](body)
if err != nil {
// Handle error
}
type ListParams struct {
Page int `query:"page" default:"1"`
Limit int `query:"limit" default:"20"`
Tags []string `query:"tags"`
SortBy string `query:"sort_by"`
}
params, err := binding.Query[ListParams](r.URL.Query())
Combine data from multiple sources:
type CreateOrderRequest struct {
// From path parameters
UserID int `path:"user_id"`
// From query string
Coupon string `query:"coupon"`
// From headers
Auth string `header:"Authorization"`
// From JSON body
Items []OrderItem `json:"items"`
Total float64 `json:"total"`
}
req, err := binding.Bind[CreateOrderRequest](
binding.FromPath(pathParams),
binding.FromQuery(r.URL.Query()),
binding.FromHeader(r.Header),
binding.FromJSON(body),
)
Follow these guides to master request data binding with Rivaas:
| Source | Function | Description |
|---|---|---|
| Query | Query[T]() | URL query parameters (?name=value) |
| Path | Path[T]() | URL path parameters (/users/:id) |
| Form | Form[T]() | Form data (application/x-www-form-urlencoded) |
| Header | Header[T]() | HTTP headers |
| Cookie | Cookie[T]() | HTTP cookies |
| JSON | JSON[T]() | JSON body |
| XML | XML[T]() | XML body |
| YAML | yaml.YAML[T]() | YAML body (sub-package) |
| TOML | toml.TOML[T]() | TOML body (sub-package) |
| MessagePack | msgpack.MsgPack[T]() | MessagePack body (sub-package) |
| Protocol Buffers | proto.Proto[T]() | Protobuf body (sub-package) |
The binding package uses Go generics for compile-time type safety:
// Generic API (preferred) - Type-safe at compile time
user, err := binding.JSON[CreateUserRequest](body)
// Non-generic API - When type comes from variable
var user CreateUserRequest
err := binding.JSONTo(body, &user)
Benefits:
For integration with rivaas/app, the Context provides a convenient Bind() method that handles all the complexity automatically.
Install and set up the Rivaas binding package for your Go application
Learn the fundamentals of binding request data to Go structs
Master URL query string binding with slices, defaults, and type conversion
Bind and parse JSON request bodies with automatic type conversion and validation
Combine multiple data sources with precedence rules for flexible request handling
Master struct tag syntax for precise control over data binding
Complete reference for all supported data types and conversions
Master error handling patterns for robust request validation and debugging
Advanced techniques including custom converters, binders, and extension patterns
Real-world examples and integration patterns for common use cases
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.