Skip to content

derekstavis/go-qs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

9eef69e · Jul 20, 2018

History

14 Commits
Jul 20, 2018
Nov 22, 2016
Nov 21, 2016
Jul 2, 2018
Jul 20, 2018
Jul 2, 2018
Nov 22, 2016
Nov 22, 2016
Jul 2, 2018
Jul 2, 2018

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