https://github.com/fpbear/querystring
QueryString is a Go library for converting a struct or a map into URL values, with support for custom encoders, time formatting, and handling of different data types.
https://github.com/fpbear/querystring
camelcase custom-encode pascalcase query-string querystring snakecase url-values
Last synced: 11 days ago
JSON representation
QueryString is a Go library for converting a struct or a map into URL values, with support for custom encoders, time formatting, and handling of different data types.
- Host: GitHub
- URL: https://github.com/fpbear/querystring
- Owner: FPbear
- License: apache-2.0
- Created: 2024-04-12T06:20:11.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-15T08:02:03.000Z (almost 2 years ago)
- Last Synced: 2024-06-19T12:26:41.897Z (over 1 year ago)
- Topics: camelcase, custom-encode, pascalcase, query-string, querystring, snakecase, url-values
- Language: Go
- Homepage:
- Size: 13.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# QueryString
QueryString is a Go library for converting a struct or a map into URL values, with support for custom encoders, time formatting, and handling of different data types.
## Installation
Use the package manager [go get](https://github.com/FPbear/querystring) to install QueryString.
```bash
go get github.com/FPbear/querystring
```
## Usage
### Example
```golang
import "github.com/FPbear/querystring"
type Input struct {
Hello string `url:"hello"`
Foo string `url:"foo,omitempty"`
Empty string `url:"empty"`
}
func main() {
input := Input{
Hello: "world",
Foo: "",
Empty: "",
}
values, err := querystring.Values(input)
if err != nil {
log.Fatal(err)
}
fmt.Println(values.Encode())
// Output: hello=world&empty=
}
```
You can also customize the encoder, for example:
```golang
import "github.com/FPbear/querystring"
type subEncode struct {
Hello string `form:"hello"`
World string `form:"world"`
}
func (s *subEncode) Encode() ([]string, error) {
return []string{s.Hello + "-" + s.World}, nil
}
type Input struct {
Hello string `form:"hello"`
Foo string `form:"foo,omitempty"`
Empty string `form:"empty"`
Sub *subEncode `form:"sub,omitempty"`
}
func main() {
input := Input{
Hello: "world",
Foo: "",
Empty: "",
Sub: &subEncode{
Hello: "hello",
World: "world",
},
}
con := NewConverter(NewTag(WithTag("form")))
values, err := con.Values(input)
if err != nil {
log.Fatal(err)
}
fmt.Println(values.Encode())
// Output: hello=world&empty=&sub=hello-world
}
```