英文:
unexpected case, expecting expression error when compiling go
问题
当我编译以下代码时,它会生成以下错误:
在第case workerAddr = <- mr.registerChannel:
行上出现语法错误:unexpected case, expecting expression
。
我不知道为什么会出现这个错误,因为我只想让workerAddr
通过任一通道进行设置。如果有任何建议,将不胜感激。
for i := 0; i < mr.nMap; i++ {
go func(jobCount) {
for {
var workerAddr string
select {
// 从registerChannel或idleChannel获取一个worker
case workerAddr = <-mr.registerChannel:
case workerAddr = <-mr.idleChannel:
}
// 省略以下代码
}
}(i)
}
英文:
When I compile the following code, it generates
*syntax error: unexpected case, expecting expression*
at line *case workerAddr = <- mr.registerChannel:*
I do not know why it happens since I just want to workerAddr to be set by either channel. Any suggestion will be appreciated.
for i:= 0; i < mr.nMap; i++ {
go func(jobCount) {
for {
var workerAddr string
Select {
// get a worker either from registerChannel or from idle channel
case workerAddr = <- mr.registerChannel:
case workerAddr = <- mr.idleChannel:
}
// omit the following code
}
}(i)
}
答案1
得分: 2
这是因为你写成了Select
,而应该写成select
。
Go语言的关键字都是小写的,所以当编译器看到一个大写的单词时,它立即知道这不是一个表达式,因此会报语法错误。
英文:
It happens because you have written Select
when you should have written select
.
None of the Go keywords are capitalized, so when the compiler sees a capitalized word, it knows immediately that it's not an expression, and therefore a syntax error.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论