Skip to content

derekstavis/go-qs

Repository files navigation

Guitar String

go-qs

Go port of Rack's query strings

Build Status

This package was written as I haven't found a good package that understands Rack/Rails query string format.

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. All tests included into test suite are also a port of Rack tests, so this package keeps great compatibility with Rack implementation.

Usage

Unmarshal

To unmarshal query strings to a map[string]interface{}:

package main

import (
  "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:

package main

import (
  "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