gorilla schema
github.com/gorilla/schema .
, , URL- , schema "schema".
:
import "github.com/gorilla/schema"
type Filter struct {
Offset int64 `schema:"offset"`
Limit int64 `schema:"limit"`
SortBy string `schema:"sortby"`
Asc bool `schema:"asc"`
Username string `schema:"username"`
First_Name string `schema:"first_name"`
Last_Name string `schema:"last_name"`
Status string `schema:"status"`
}
func MyHandler(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
}
filter := new(Filter)
if err := schema.NewDecoder().Decode(filter, r.Form); err != nil {
}
fmt.Printf("%+v", filter)
}
json
, schema .
[]string ( ), schema.
Request.Form - map, string []string ( URL:
Form url.Values
url.Values:
type Values map[string][]string
, , Filter :
type Filter struct {
Offset []string `json:"offset"`
Limit []string `json:"limit"`
SortBy []string `json:"sortby"`
}
json r.Form, :
func MyHandler(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
}
data, err := json.Marshal(r.Form)
if err != nil {
}
filter := new(Fiter)
if err = json.Unmarshal(data, filter); err != nil {
}
fmt.Printf("%+v", filter)
}
, . , , "" r.Form map string []string.
:
type Filter struct {
Offset string `json:"offset"`
Limit string `json:"limit"`
SortBy string `json:"sortby"`
}
m := map[string]string{}
for k, v := range r.Form {
m[k] = v[0]
}
m Filter .