将标志变量传递给Go程序会导致奇怪的输出。

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

Passing flag variable to go program causing strange output

问题

sergiotapia在Macbook-Air上的~/Work/go/src/github.com/sergiotapia/gophers目录下,正在执行以下命令:

go build && go install && gophers -github_url=https://github.com/search?utf8=%E2%9C%93&q=location%3A%22San+Fransisco%22+location%3ACA+followers%3A%3E100&type=Users&ref=advsearch&l=

输出结果如下:

[1] 51873
[2] 51874
[3] 51875
[4] 51877
[2]   Done                    q=location%3A%22San+Fransisco%22+location%3ACA+followers%3A%3E100
[3]   Done                    type=Users
[4]+  Done                    ref=advsearch

这段代码中,我试图将长的GitHub URL作为代码中的参数使用,用于Gophers。对于其他类型的URL,如组织或stargazers,它都可以正常工作。然而,当我尝试使用搜索结果页面时,会得到上面奇怪的输出。

这是要翻译的内容。

英文:
sergiotapia at Macbook-Air in ~/Work/go/src/github.com/sergiotapia/gophers on master [!]
$ go build && go install && gophers -github_url=https://github.com/search?utf8=%E2%9C%93&q=location%3A%22San+Fransisco%22+location%3ACA+followers%3A%3E100&type=Users&ref=advsearch&l=
[1] 51873
[2] 51874
[3] 51875
[4] 51877
[2]   Done                    q=location%3A%22San+Fransisco%22+location%3ACA+followers%3A%3E100
[3]   Done                    type=Users
[4]+  Done                    ref=advsearch

I'm trying to use the long github url as a parameter in my code for Gophers. It works fine for all other url types such as organisations or stargazers. However when I try to use the search results page I get the strange output above.

https://github.com/search?utf8=%E2%9C%93&q=location%3A%22San+Fransisco%22+location%3ACA+followers%3A%3E100&type=Users&ref=advsearch&l=

package main

import (
	"flag"
	"log"
	"strings"

	"github.com/PuerkitoBio/goquery"
)

type user struct {
	name     string
	email    string
	url      string
	username string
}

func main() {
	url := flag.String("github_url", "", "github url you want to scrape")
	flag.Parse()
	githubURL := *url
	doc, err := goquery.NewDocument(githubURL)
	if err != nil {
		log.Fatal(err)
	}

	if strings.Contains(githubURL, "/orgs/") {
		scrapeOrganization(doc, githubURL)
	} else if strings.Contains(githubURL, "/search?") {
		scrapeSearch(doc, githubURL)
	} else if strings.Contains(githubURL, "/stargazers") {
		scrapeStarGazers(doc, githubURL)
	} else {
		scrapeProfile(doc)
	}
}

答案1

得分: 5

这是一个bash命令行(或者mac使用的命令行)。&?是必须转义的shell元字符。Shell对URL一无所知,也不应该知道。

go 'http://....'
   ^-----------^

添加引号将阻止shell解析元字符。另一种方法是手动转义每个元字符:

go http://example.com/script.php\?foo=bar\&baz=qux
                                ^--------^

这种方法很快变得乏味且容易出错。

英文:

It's a bash command line (or whatever the mac uses). & and ? are shell metacharacters that you MUST escape. The shell has absolutely no idea what a URL is, nor should it ever have to.

go 'http://....'
   ^-----------^

Adding quotes will prevent the shell from parsing the metacharacters. The alternative is to manually escape each and ever metachar yourself:

go http://example.com/script.php\?foo=bar\&baz=qux
                                ^--------^

which quickly gets tedious, and error prone.

huangapple
  • 本文由 发表于 2015年4月14日 05:14:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/29615232.html
匿名

发表评论

匿名网友

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

确定