Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/derekstavis/go-qs
A Go port of Rack's query string parser
https://github.com/derekstavis/go-qs
go golang parser query querystring url urlencoded
Last synced: about 2 months ago
JSON representation
A Go port of Rack's query string parser
- Host: GitHub
- URL: https://github.com/derekstavis/go-qs
- Owner: derekstavis
- License: mit
- Created: 2016-11-21T13:20:43.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-07-20T19:22:46.000Z (over 6 years ago)
- Last Synced: 2024-06-19T15:46:05.209Z (6 months ago)
- Topics: go, golang, parser, query, querystring, url, urlencoded
- Language: Go
- Homepage:
- Size: 147 KB
- Stars: 100
- Watchers: 6
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-qs
Go port of Rack's query strings
[![Build Status](https://travis-ci.org/derekstavis/go-qs.svg?branch=master)](https://travis-ci.org/derekstavis/go-qs)
This package was written as I haven't found a good package that understands
[Rack/Rails](http://guides.rubyonrails.org/form_helpers.html#understanding-parameter-naming-conventions) query string [format](https://gist.github.com/dapplebeforedawn/3724090).It have been designed to marshal and unmarshal nested query strings from/into
`map[string]interface{}`, inspired on the interface of Go builtin `json`
package.## Compatibility
`go-qs` is a port of [Rack's code](https://github.com/rack/rack/blob/rack-1.3/lib/rack/utils.rb#L114).
All tests included into [test suite](https://github.com/derekstavis/go-qs/blob/master/marshal_test.go)
are also a port of [Rack tests](https://github.com/rack/rack/blob/rack-1.3/test/spec_utils.rb#L107),
so this package keeps great compatibility with Rack implementation.## Usage
### Unmarshal
To unmarshal query strings to a `map[string]interface{}`:
```go
package mainimport (
"fmt"
"github.com/derekstavis/go-qs"
)query, err := qs.Unmarshal("foo=bar&names[]=foo&names[]=bar")
if err != nil {
fmt.Printf("%#+v\n", query)
}
```The output:
```
map[string]interface {}{"foo":"bar", "names":[]interface {}{"foo", "bar"}}
```### Marshal
You can also marshal a `map[string]interface{}` to a query string:
```go
package mainimport (
"fmt"
"github.com/derekstavis/go-qs"
)payload := map[string]interface {}{"foo":"bar", "names":[]interface {}{"foo", "bar"}}
querystring, err := qs.Marshal(payload)
if err != nil {
fmt.Printf(querystring)
}
```The output:
```
foo=bar&names[]=foo&names[]=bar
```## License
```
MIT Copyright (c) 2016 Derek W. Stavis
```