Parse and decode URL query strings into nested Go data structures. Compatible with javascript qs.
go-qs
is a work in progress Go library for parsing URL query strings into flexible, deeply nested Go maps and slices. It supports arrays, objects, numeric indices, and customizable parsing options, making it ideal for web applications and APIs.
go get github.com/globocom/go-qs
package main
import (
"fmt"
"github.com/globocom/go-qs"
)
func main() {
result, err := goqs.Parse("user[name]=Alice&user[age]=30", nil)
if err != nil {
panic(err)
}
fmt.Println(result)
// Output: map[user:map[age:30 name:Alice]]
}
- Parse query strings into
map[string]interface{}
- Support for arrays:
arr[]=1&arr[]=2
- Support for nested objects:
user[name]=Alice&user[age]=30
- Numeric indices:
a[1]=1&a[2]=2
- Customizable parsing options (array limits, depth, charset, etc.)
- Handles empty values, custom delimiters, and more
Customize parsing behavior using ParseOptions
:
opts := &goqs.ParseOptions{
AllowDots: true,
ArrayLimit: 20,
Depth: 5,
// ...other options
}
result, err := goqs.Parse("a.b=1&a.c=2", opts)
See ParseOptions for all available fields.
Unit tests are provided in parse_test.go
covering various scenarios:
- Empty queries
- Simple parameters
- Arrays and nested objects
- Numeric indices
Run tests with:
go test
We welcome contributions! Please see our CONTRIBUTING.md for guidelines on pull requests and issues, including best practices for reporting bugs and comparing with the JavaScript qs
library.
This project is licensed under the MIT License. See the LICENSE file for details.
This project was inspired by ljharb/qs, a popular query string parser for JavaScript.
For more details, see the GoDoc.
Made with love ❤️ and ☕ by Backstage