How to do interpolation on URL in Golang

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

How to do interpolation on URL in Golang

问题

我有以下URL,并且我想要在每次迭代中更改start的值。有没有更好的方法来做到这一点?

test := "https://www.googleapis.com/customsearch/v1?start=%d&cx=001106611627702700888%3Aaonktv-oz_w&q=bells%20palsy%20mouth&exactTerms=palsy&fileType=png&imgColorType=color&imgType=face&searchType=image&key=AIzaSyAYqQ4IxUHnF7rfvzSvnczxQ-u93AbkC8k"

for v := 1; v < 100; v += 10 {
    val := fmt.Sprintf(test, v)
    fmt.Println(val)
}

当前输出为:

https://www.googleapis.com/customsearch/v1?start=1&amp;cx=001106611627702700888%!A(MISSING)aonktv-oz_w&amp;q=bells%!p(MISSING)alsy%!m(MISSING)outh&amp;exactTerms=palsy&amp;fileType=png&amp;imgColorType=color&amp;imgType=face&amp;searchType=image&amp;key=AIzaSyAYqQ4IxUHnF7rfvzSvnczxQ-u93AbkC8k

预期输出应为:

https://www.googleapis.com/customsearch/v1?startindex=1&amp;q=bells%20palsy%20mouth
https://www.googleapis.com/customsearch/v1?startindex=11&q=bells%20palsy%20mouth

为什么Sprintf给我返回(MISSING)和一些随机字符?

英文:

I have the follow URL and I want to change the value of start for each iteration. Is there a better way to do it?

	test := &quot;https://www.googleapis.com/customsearch/v1?start=%d&amp;cx=001106611627702700888%3Aaonktv-oz_w&amp;q=bells%20palsy%20mouth&amp;exactTerms=palsy&amp;fileType=png&amp;imgColorType=color&amp;imgType=face&amp;searchType=image&amp;key=AIzaSyAYqQ4IxUHnF7rfvzSvnczxQ-u93AbkC8k&quot;

for v := 1; v &lt; 100; v += 10 {
	val := fmt.Sprintf(test, v)
    fmt.Println(val)
}

Output now:

https://www.googleapis.com/customsearch/v1?start=1&amp;cx=001106611627702700888%!A(MISSING)aonktv-oz_w&amp;q=bells%!p(MISSING)alsy%!m(MISSING)outh&amp;exactTerms=palsy&amp;fileType=png&amp;imgColorType=color&amp;imgType=face&amp;searchType=image&amp;key=AIzaSyAYqQ4IxUHnF7rfvzSvnczxQ-u93AbkC8k

Expected Output should be:

https://www.googleapis.com/customsearch/v1?startindex=1&amp;q=bells%20palsy%20mouth
https://www.googleapis.com/customsearch/v1?startindex=11&amp;q=bells%20palsy%20mouth

....etc.

Why Sprintf gives me (MISSING) and a couple of random characters?

答案1

得分: 3

非格式化动词的%字符应该被转义为%%

test := "https://www.googleapis.com/customsearch/v1?start=%d&amp;cx=001106611627702700888%%3Aaonktv-oz_w&amp;q=bells%%20palsy%%20mouth&amp;exactTerms=palsy&amp;fileType=png&amp;imgColorType=color&amp;imgType=face&amp;searchType=image&amp;key=AIzaSyAYqQ4IxUHnF7rfvzSvnczxQ-u93AbkC8k"

playground示例

如果%没有被转义,那么fmt期望找到相应的参数,并在找不到参数时输出(MISSING)

英文:

The % characters that are not part of a format verb should be escaped as %%:

test := &quot;https://www.googleapis.com/customsearch/v1?start=%d&amp;cx=001106611627702700888%%3Aaonktv-oz_w&amp;q=bells%%20palsy%%20mouth&amp;exactTerms=palsy&amp;fileType=png&amp;imgColorType=color&amp;imgType=face&amp;searchType=image&amp;key=AIzaSyAYqQ4IxUHnF7rfvzSvnczxQ-u93AbkC8k&quot;

playground example

If the % are not escaped, then fmt expects to find a corresponding argument and complains with the output (MISSING) when the argument is not found.

huangapple
  • 本文由 发表于 2016年4月19日 01:13:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/36700291.html
匿名

发表评论

匿名网友

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

确定