Encode struct to querystring?

5,099 views
Skip to first unread message

Ian Rose

unread,
Apr 16, 2014, 11:24:24 AM4/16/14
to golan...@googlegroups.com
Howdy -

I'm looking for something like the opposite of gorilla/schema (which parses URL parameters into a struct) - I have a struct and I want to construct a map of URL parameters from its fields.  Is there an easy way to do that?

Cheers,
Ian

Stanley Shilov

unread,
Apr 17, 2014, 12:10:15 AM4/17/14
to golan...@googlegroups.com
If I understand you correctly, you could use the url package to Add the fields one by one then use Encode() to convert the map into a query string.

Hope this helps.

roj...@gmail.com

unread,
Apr 17, 2014, 1:14:57 PM4/17/14
to golan...@googlegroups.com
As Stanley suggested the net/url package is probably what you are looking for. There is also this package which I think is very useful:
https://godoc.org/github.com/google/go-querystring/query

Ian Rose

unread,
Apr 18, 2014, 9:06:35 AM4/18/14
to roj...@gmail.com, golang-nuts
net/url doesn't get the job done (you'd have to manually copy each struct field into the url.Values map, which defeats the purpose) but that go-querystring package looks like it will serve nicely.

Thanks!


--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/M4MZJtjYbPg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages