urllib.quote() in Go?

huangapple go评论81阅读模式
英文:

urllib.quote() in Go?

问题

在Go语言中,没有与Python中的urllib.quote(string)函数完全相同的功能。然而,你可以使用url.QueryEscape()函数来对字符串进行URL编码。这个函数可以将字符串中的特殊字符转换为URL安全的形式。你可以在Go的官方文档中找到更多关于url包的信息:https://golang.org/pkg/net/url/

英文:

Is there any function in Go works the same as urllib.quote(string) in Python? Thank you!
The document page for urllib.quote(): https://docs.python.org/2/library/urllib.html

答案1

得分: 2

urllib.quote 是用于对URL的路径部分进行编码的。Go语言的net/url包没有直接暴露这个功能,但你可以通过一种迂回的方式来实现:

func quote(s string) string {
    return (&url.URL{Path: s}).RequestURI()
}

由于Python的函数进行了比必要更多的转义,因此这里的quote函数和urllib.quote不总是会得到相同的结果。

Go的QueryEscape提供了与Python的urlib.quote_plus相同的功能。

英文:

urllib.quote is designed to quote the path section of a URL. Go's net/url package does not expose this functionality directly, but you can get at it in a roundabout way:

func quote(s string) string {
    return (&url.URL{Path: s}).RequestURI()
}

Because the Python function escapes more than it needs to, the quote function here and urllib.quote will not always give the same results.

Go's QueryEscape provides the same functionality as Python's urlib.quote_plus.

答案2

得分: 1

url.QueryEscape

http://play.golang.org/p/yNZZT-Xmfs

func main() {
    fmt.Println(url.QueryEscape("/Hello, playground"))
}

// %2FHello%2C+playground
英文:

url.QueryEscape

http://play.golang.org/p/yNZZT-Xmfs

func main() {
	fmt.Println(url.QueryEscape("/Hello, playground"))
}

// %2FHello%2C+playground

答案3

得分: 0

自从1.8版本以来,Go语言提供了url.PathEscape函数,用于对URL的路径部分进行转义,就像Python中的urllib.quote(string)函数一样。

英文:

Since version 1.8, Go has url.PathEscape to quote the path section of a URL just as urllib.quote(string) does for Python.

huangapple
  • 本文由 发表于 2014年12月19日 06:24:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/27556773.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定