编译Go时出现意外情况,预期为表达式错误。

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

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 = &lt;- 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 &lt; mr.nMap; i++ {	
	go func(jobCount) {
		for {
			var workerAddr string
			Select {
				// get a worker either from registerChannel or from idle channel
			    case workerAddr = &lt;- mr.registerChannel:
			    case workerAddr = &lt;- 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.

huangapple
  • 本文由 发表于 2017年4月8日 06:46:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/43288131.html
匿名

发表评论

匿名网友

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

确定