Golang percent encoding

colecmac at protonmail.com colecmac at protonmail.com
Mon Jun 22 22:36:16 BST 2020


Just wanted to let other Gemini Go coders know something about encoding
query strings. The Gemini spec mandates percent coding, so you should be
using url.PathEscape and url.PathUnescape to deal with queries, and NOT
url.QueryEscape and url.QueryUnescape.

However, there's one more thing. url.PathEscape does not replace +
characters, although to ensure compatibility, it probably should. Here's
the function I use in all my projects to escape queries:

func pathEscape(path string) string {
	return strings.ReplaceAll(url.PathEscape(path), "+", "%2B")
}

This is simple, but seems to work perfectly. url.PathUnescape will still
work fine for decoding.

Cheers,
makeworld




More information about the Gemini mailing list