将标志变量传递给程序导致奇怪的输出
Passing flag variable to go program causing strange output
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
我试图在 Gophers 的代码中使用长 github url 作为参数。它适用于所有其他 url 类型,例如组织或观星者。但是,当我尝试使用搜索结果页面时,我得到了上面的奇怪输出。
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)
}
}
这是一个 bash 命令行(或 mac 使用的任何命令行)。 &
和 ?
是您必须转义的 shell 元字符。 shell 完全不知道 URL 是什么,也不应该知道。
go 'http://....'
^-----------^
添加引号将阻止 shell 解析元字符。另一种方法是自己手动转义每个元字符:
go http://example.com/script.php\?foo=bar\&baz=qux
^--------^
这很快就会变得乏味且容易出错。
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
我试图在 Gophers 的代码中使用长 github url 作为参数。它适用于所有其他 url 类型,例如组织或观星者。但是,当我尝试使用搜索结果页面时,我得到了上面的奇怪输出。
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)
}
}
这是一个 bash 命令行(或 mac 使用的任何命令行)。 &
和 ?
是您必须转义的 shell 元字符。 shell 完全不知道 URL 是什么,也不应该知道。
go 'http://....'
^-----------^
添加引号将阻止 shell 解析元字符。另一种方法是自己手动转义每个元字符:
go http://example.com/script.php\?foo=bar\&baz=qux
^--------^
这很快就会变得乏味且容易出错。