许多编程语言,如JavaScript和PHP,都有一个urlencode()
函数,可以用来对查询字符串参数的内容进行编码。
"example.com?value=" + urlencode("weird string & number: 123")
这可以确保&
,%
,空格等都会被编码,以便使您的URL保持有效。
我发现golang提供了一个URL包,并且它有一个Encode()函数用于查询字符串值。这很好用,但在我的情况下,这将要求我解析URL,而我宁愿不这样做。
我的URL由客户端声明,而不更改参数的顺序和可能重复的参数(这在URL中是合法的)可能会受到影响。所以我使用Replace()
,像这样:
func tweak(url string) string {
url.Replace("@VALUE@", new_value, -1)
return url
}
期望将@VALUE@
用作查询字符串参数的值,如下所示:
example.com?username=@VALUE@
所以,我的想法是这样的: url.Replace("@VALUE@", urlencode(new_value), -1)
在Golang中是否有这样的函数可以轻松访问?
map[string][]string
,因此可以实现)。我从未见过参数的顺序有影响,但是如果您解析URL(也由于使用映射),Go不一定会保留它们。 - Peter