英文:
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)
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?
答案1
得分: 7
使用这行代码:
for _ = range flag.Args() {
如果你省略对空白标识符的初始化,错误应该会消失。
英文:
Use this line:
for _ = range flag.Args() {
The error should disappear if you omit initialization for the blank identifier.
答案2
得分: 7
:= 是一个短变量声明。_ 不是一个真正的变量,所以你不能声明它。
当你没有任何新变量时,应该使用 =。
英文:
:= 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.
答案3
得分: 4
截至Go 1.4(当前版本),您可以直接使用for range flag.Args() { ... }来跳过_ = 部分。
英文:
An update for this question, as of Go 1.4 (current tip), you can use for range flag.Args() { ... } directly skipping the _ = part.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论