Golangでmapからquerystringを生成する

キーでソートして、値はエスケープする

package main

import (
	"fmt"
	"net/url"
	"sort"
	"strings"
)

type SortedQuery struct {
	_map map[string]string
	keys []string
}

func (sq *SortedQuery) Len() int {
	return len(sq._map)
}

func (sq *SortedQuery) Less(i, j int) bool {
	return sq.keys[i] < sq.keys[j]
}

func (sq *SortedQuery) Swap(i, j int) {
	sq.keys[i], sq.keys[j] = sq.keys[j], sq.keys[i]
}

func (sq *SortedQuery) String() string {

	sort.Sort(sq)
	values := make([]string, len(sq.keys))
	for i, key := range sq.keys {
		values[i] = fmt.Sprintf("%s=%s", key, url.QueryEscape(sq._map[key]))
	}
	return strings.Join(values, "&")
}

func NewSortedQuery(m map[string]string) *SortedQuery {
	sq := new(SortedQuery)
	sq._map = m
	sq.keys = make([]string, len(m))
	i := 0
	for key, _ := range m {
		sq.keys[i] = key
		i++
	}
	return sq
}

func main() {
	query := make(map[string]string)
	query["foo"] = "bar"
	query["hoge"] = "fuga"
	query["search"] = "あいうえお"

	sorted_query := NewSortedQuery(query)
	fmt.Println(sorted_query.String())
	// foo=bar&hoge=fuga&search=%E3%81%82%E3%81%84%E3%81%86%E3%81%88%E3%81%8A

}


python2

params = {
    'foo': 'bar',
    'hoge': 'fuga',
    'search': u'あいうえお'
}

values = []
for k in sorted(params):
    values.append("{key}={value}".format(key=k,
                                         value=quote(params[k].encode('utf-8'))))

print '&'.join(values)
# foo=bar&hoge=fuga&search=%E3%81%82%E3%81%84%E3%81%86%E3%81%88%E3%81%8A