RFC 3986 encoding with GO

Hi,

I am new to Golang world, I just started my new project with Golang, I would like to know the following now,

  1. Is there any built in function for sorting the key value pair, in asc order of key, where key is string, if yes how
  2. I would like to url encode a string with RFC 3986, when I tried with Encode function by, passing argument as urlStr.Query(), its getting encoded but “space” was encoding to ‘+’, I would like to encode to ‘%20’ instead ‘+’ . Dont know whether this is the proper method to use for url encode. Please help me, if there is any other built in function/ solution for url encode with RFC 3986

Thanks

No. You do this by creating a slice for the keys, adding the keys to the slice, and sorting the slice. It’s about five lines of code.

%20 is a perfectly appropriate way to encode spaces. Possibly you might get away with just replacing %20 with + in the result, although think through if there are cases where this does work due to escaping or similar (I haven’t thought it through).

This only you can answer :slight_smile:

1 Like

Thanks @calmh.
Regarding the encoding space, I used the function strings.Replace, after encoding. it works. But suppose if there is already a char ‘+’ is there in string, before encoding, then it will be a problem rt?( actually in RFC 3986, ‘+’ should be encoded to ‘%2B’ rt? here in my case after encoding also ‘+’ remains as ‘+’ )
Will there any encoding built in function to encode RFC3986

I only skimmed it quickly, but if that is just be normal URL encoding, the String() method on any url.URL should be fine I think.

You need to create a Url, get the query, add the values and then call encode, pretty much like this: https://golang.org/pkg/net/url/#example_URL

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.