URL转义产生了”%A(MISSING)”而不是”%3A”。

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

URL Escaping producing "%A(MISSING)" instead of "%3A"

问题

我正在使用Go语言的revel框架。最近,当我运行以下代码时:

import (
    ...
    "net/url"
    ...
)

revel.INFO.Println(url.QueryEscape("http://hello.com"))

我得到的输出是:

INFO  2014/07/09 14:58:34 user.go:39: http%A(MISSING)%F(MISSING)%F(MISSING)hello.com

而我期望得到的是:

INFO  2014/07/09 14:58:34 user.go:39: http%3A%2F%2Fhello.com

为什么输出中的 %3A 被替换为 %A(MISSING),我该如何修复它?

我唯一看到可能产生字符串 "(MISSING)" 的Go代码是在 fmt 包中,但从查看 net/url 源代码包中,我不明白为什么会发生这种情况。我可能在访问旧版本(有问题的版本)的Go库吗?还是我的设置可能出了其他问题?

相关链接:https://stackoverflow.com/questions/13820280/encode-decode-urls

相关的Go源代码:http://golang.org/src/pkg/net/url/url.go?s=14330:14361#L553

英文:

I am using the revel framework with the go language. Recently, when I run the following code:

import (
    ...
    "net/url"
    ...
)

revel.INFO.Println(url.QueryEscape("http://hello.com"))

I get

INFO  2014/07/09 14:58:34 user.go:39: http%A(MISSING)%F(MISSING)%F(MISSING)hello.com

When I expect to get something more like

INFO  2014/07/09 14:58:34 user.go:39: http%3A%2F%2Fhello.com

Why has %3A been replaced by %A(MISSING) in the output and how can I fix it?

The only go code where I see something that might produce the string "(MISSING)' is in the fmt package, but from looking at the net/url source code package, I don't see how that could be happening. Am I perhaps somehow accessing an old (and broken?) version of the go libraries? Is something else possibly wrong with my setup?

Related: https://stackoverflow.com/questions/13820280/encode-decode-urls

Relevant Go Source code: http://golang.org/src/pkg/net/url/url.go?s=14330:14361#L553

答案1

得分: 7

revel.INFO.Println 类似于 fmt.Printf,它需要一个格式字符串和参数。如果要记录包含 % 字符的字符串,你可以选择对其进行转义,或者更好地将其作为参数传递:

revel.INFO.Println("转义后的URL是:%s", url.QueryEscape("http://hello.com"))

(你也可以只使用 "%s" 作为格式字符串,但为什么不趁机提供上下文呢?)

英文:

revel.INFO.Println is like fmt.Printf, it expects a format string and arguments. To log a string that contains % characters you either need to escape it, or better pass it as an argument:

revel.INFO.Println("The escaped URL is: %s", url.QueryEscape("http://hello.com"))

(You could just use "%s" as the format string, but why not take the chance to provide context.)

huangapple
  • 本文由 发表于 2014年7月10日 03:06:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/24661862.html
匿名

发表评论

匿名网友

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

确定