开发者

Why can't I use the blank identifier in the following Go code?

for _, arg := range flag.Args() {
    go func() {
        path.Walk(arg, dupes, walkerrs)
       开发者_JS百科 walkend <- true
    }()
}
for _ := range flag.Args() {
    if !<-walkend {
        os.Exit(1)
    }
}

The second use of _ gives this error: no new variables on left side of :=. What have I done wrong?


Use this line:

for _ = range flag.Args() {

The error should disappear if you omit initialization for the blank identifier.


:= is a short variable declaration. _ is not a real variable, so you can't declare it.

You should use = instead, when you don't have any new variables.


An update for this question, as of Go 1.4 (current tip), you can use for range flag.Args() { ... } directly skipping the _ = part.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜