Skip to content

Latest commit

History

15 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Params

BadgeLICENSE

Params is Go library for convert url.Values to struct, support Http request query-parameters, form-parameters.


Installation

Use go get to install:

gogetgithub.com/youkale/params

Usage

  1. 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"`
}
  1. 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.

Tag Options

  • param:"store_id,store_01": The following options can be included in the param tag.
  • 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.

Performance

 goos: linux
goarch: amd64
pkg: github.com/youkale/params
2000000000 0.00 ns/op
PASS

License

LICENSE

About

Params is Go library for convert url.Values to struct

Resources

Stars

19 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages