Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/youkale/params
Params is Go library for convert url.Values to struct
https://github.com/youkale/params
Last synced: 29 days ago
JSON representation
Params is Go library for convert url.Values to struct
- Host: GitHub
- URL: https://github.com/youkale/params
- Owner: youkale
- License: other
- Created: 2018-01-11T03:39:56.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-03-16T03:29:24.000Z (over 1 year ago)
- Last Synced: 2024-10-04T13:31:24.345Z (about 2 months ago)
- Language: Go
- Homepage:
- Size: 16.6 KB
- Stars: 19
- Watchers: 1
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Params
---
[![Badge](https://img.shields.io/badge/link-996.icu-%23FF4D5B.svg?style=flat-square)](https://996.icu/#/en_US)
[![LICENSE](https://img.shields.io/badge/license-Anti%20996-blue.svg?style=flat-square)](https://github.com/996icu/996.ICU/blob/master/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
go get github.com/youkale/params
```Usage
1. Define a struct with tags that match the query parameter names:```go
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"`
}```
2. In your HTTP handler, parse the query parameters into an instance of the struct:
```go
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](https://img.shields.io/badge/license-Anti%20996-blue.svg)](https://github.com/996icu/996.ICU/blob/master/LICENSE)