Can I count on the ordering of an Encode()d url.Values?

1,012 views
Skip to first unread message

Caleb Spare

unread,
Oct 24, 2013, 12:05:48 AM10/24/13
to golan...@googlegroups.com
Hi all,

A url.Values can write itself out in normal querystring parameter form (a=b&c=d) using Encode(). The entire documentation for that method is:

Encode encodes the values into “URL encoded” form. e.g. "foo=bar&bar=baz"

For my application, I'm using the string representation of querystring parameters as a cache key; I need it to be stable. Now, url.Values is a map, and nothing about the ordering is mentioned in the Encode() documentation, but it so happens that the implementation of Encode() does sort the keys before writing them out:


Can I depend on this ordering to remain stable? (The answer is probably no, since it's not a documented contract.) Does it make sense to specify the ordering as part of the Encode() documentation?

One argument against it might be that leaving it unspecified leaves open the option of removing the sorting as a performance optimization.

As a side note, the example ("foo=bar&bar=baz") will actually never occur with the current implementation, since "bar" precedes "foo". (But maybe that's a subtle way of hinting that the keys will not necessarily be sorted...)

-Caleb

David Symonds

unread,
Oct 24, 2013, 12:08:46 AM10/24/13
to Caleb Spare, golang-nuts
I don't think it would be wise to depend on it. It's easy enough to do
your own encoding and then sort the list before joining on &.

Andrew Gerrand

unread,
Oct 24, 2013, 12:21:07 AM10/24/13
to David Symonds, Caleb Spare, golang-nuts
Our tests depend on the ordering, so I think it's reasonable for users to depend on it. I don't think we will change it for Go 1.x.

I'll fix the documentation.

Andrew


On 24 October 2013 08:08, David Symonds <dsym...@golang.org> wrote:
I don't think it would be wise to depend on it. It's easy enough to do
your own encoding and then sort the list before joining on &.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Caleb Spare

unread,
Oct 24, 2013, 3:15:44 AM10/24/13
to golan...@googlegroups.com, David Symonds, Caleb Spare
Thanks Andrew!
Reply all
Reply to author
Forward
0 new messages