Params is Go library for convert url.Values to struct, support Http request query-parameters, form-parameters.
Use go get to install:
gogetgithub.com/youkale/paramsUsage
- Define a struct with tags that match the query parameter names:
typeUserstruct {
UserIdint64`param:"user_id,100"`StoreIdint`param:"store_id"`Pagefloat32`param:"page"`Namestring`param:"name"`Ageuint8`param:"age,18"`Enablebool`param:"enable,false"`
}- In your HTTP handler, parse the query parameters into an instance of the struct:
import"github.com/youkale/params"funcMyHandler(w http.ResponseWriter, r*http.Request) {
// or convert request.FormvaruserUseriferr:=params.Convert(r.URL.Query(), &user); err!=nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Do something with the user struct...
}Params will automatically parse the query parameters and set the values in the struct fields.
param:"store_id,store_01": The following options can be included in theparamtag.store_id: Indicates the key to get the content of the field.store_01: specifies a default value for the field if the query parameter is not present.
goos: linux
goarch: amd64
pkg: github.com/youkale/params
2000000000 0.00 ns/op
PASS