Skip to content

Instantly share code, notes, and snippets.

@hnaohiro
Created January 24, 2013 20:57
Show Gist options
  • Save hnaohiro/4627658 to your computer and use it in GitHub Desktop.
Save hnaohiro/4627658 to your computer and use it in GitHub Desktop.
Golang URL Encode
func urlencode(s string) (result string){
for _, c := range(s) {
if c <= 0x7f { // single byte
result += fmt.Sprintf("%%%X", c)
} else if c > 0x1fffff {// quaternary byte
result += fmt.Sprintf("%%%X%%%X%%%X%%%X",
0xf0 + ((c & 0x1c0000) >> 18),
0x80 + ((c & 0x3f000) >> 12),
0x80 + ((c & 0xfc0) >> 6),
0x80 + (c & 0x3f),
)
} else if c > 0x7ff { // triple byte
result += fmt.Sprintf("%%%X%%%X%%%X",
0xe0 + ((c & 0xf000) >> 12),
0x80 + ((c & 0xfc0) >> 6),
0x80 + (c & 0x3f),
)
} else { // double byte
result += fmt.Sprintf("%%%X%%%X",
0xc0 + ((c & 0x7c0) >> 6),
0x80 + (c & 0x3f),
)
}
}
return result
}
@flisky
Copy link

flisky commented May 26, 2014

FYI, offical version should be url.QueryEscape

@xsleonard
Copy link

url.QueryEscape encodes spaces as +, but if you need e.g. a path encoded, you can do

u := &url.URL{Path: path}
encodedPath := u.String()

@richmondwang
Copy link

it escapes everything?

@theory
Copy link

theory commented Oct 1, 2016

What if you need a path component with a slash? In Perl:

> perl -MURI -E 'use utf8; my $uri = URI->new("http://example.com"); $uri->path_segments(qw(tart user foo/bâr profile)); say $uri->canonical;'          
http://example.com/tart/user/foo%2Fb%C3%A2r/profile

This lets me specify each path part separately.

@callicoder
Copy link

callicoder commented Dec 9, 2018

Why is this required? Golang already has QueryEscape() as well as PathEscape() functions. I've written about URL Encoding in Golang and URL Decoding in Golang. Also, there are tools to URL Encode online and URL Decode online.

@dnutels
Copy link

dnutels commented May 26, 2019

@callicoder

It's needed because Golang does this:

  1. use url.PathEscape and get space as %20, but leave : unencoded (should be %3A), or
  2. use url.QueryEscape and get space as +, but encode : as %3A

So, if you integrate with something that goes by RFC in a certain manner and encodes %20 and %3A, you are out of luck.

P.S. Of course, : is semi-legal and perhaps shouldn't be encoded, but tell that to all kinds of Java/Python libs...

@UndeadBaneGitHub
Copy link

UndeadBaneGitHub commented Oct 31, 2022

To add a cherry on top: QueryEscape incorrectly escapes already escaped strings over and over again, which is straight up ridiculous.

It's 2022 and it still does this.

https://go.dev/play/p/Lwu-ixIwMB3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment