/params

Params is Go library for convert url.Values to struct

Primary LanguageGoOtherNOASSERTION

Params

Badge LICENSE

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


Installation

Use go get to install:

 go get github.com/youkale/params

Usage

  1. Define a struct with tags that match the query parameter names:
type User struct {
    UserId  int64   `param:"user_id,100"`
    StoreId int     `param:"store_id"`
    Page    float32 `param:"page"`
    Name    string  `param:"name"`
    Age     uint8   `param:"age,18"`
    Enable  bool    `param:"enable,false"`
}
  1. In your HTTP handler, parse the query parameters into an instance of the struct:
import "github.com/youkale/params"

func MyHandler(w http.ResponseWriter, r *http.Request) {
    // or convert request.Form
    var user User
    if err := 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