在大括号或返回语句周围出现了奇怪的错误。

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

golang ,strange error around braces or return

问题

func isPrimeNumber(possiblePrime int) bool {
for underPrime := 2; underPrime < possiblePrime; underPrime++ {
if possiblePrime%underPrime == 0 {
return false
}
}
return true
}

func findPrimeNumbers(channel chan int) {
for i := 2; ; /* 无限循环 */ i++ {
// 你的代码放在这里
if isPrimeNumber(i){
channel <- i // 在这一行出现错误
}
assert(i < 100) // i 害怕高度
}
}

我在这一行出现错误,但是无法找出问题所在,需要帮助。谢谢。

英文:
  1. func isPrimeNumber(possiblePrime int) bool {
  2. for underPrime := 2; underPrime &lt; possiblePrime; underPrime++ {
  3. if possiblePrime%underPrime == 0 {
  4. return false
  5. }
  6. }
  7. return true
  8. }
  9. func findPrimeNumbers(channel chan int) {
  10. for i := 2; ; /* infinite loop */ i++ {
  11. // your code goes here
  12. if isPrimeNumber(i){
  13. chan &lt;- i &lt;========error on this line
  14. }
  15. assert(i &lt; 100) // i is afraid of heights
  16. }
  17. }

I got error on this but could not figure it out, need help. thanks

syntax error: unexpected semicolon or newline, expecting {
FAIL

答案1

得分: 1

使用channel <- i而不是chan <- i

在函数定义中(channel chan int),channel是参数的名称,chan int是类型。为了澄清,您的函数可以重写为以下形式:

  1. func findPrimeNumbers(primeNumberChannel chan int) {
  2. for i := 2; ; i++ {
  3. if isPrimeNumber(i){
  4. primeNumberChannel <- i
  5. }
  6. }
  7. }

另外,在Go语言中没有assert语句(http://golang.org/doc/faq#assertions)。

英文:

Use channel &lt;- i instead of chan &lt;- i.

In you function definition (channel chan int), channel is parameter's name, and chan int is the type. To clarify, your function could be rewrote to the following one:

  1. func findPrimeNumbers(primeNumberChannel chan int) {
  2. for i := 2; ; i++ {
  3. if isPrimeNumber(i){
  4. primeNumberChannel &lt;- i
  5. }
  6. }
  7. }

Additionally, assert is not available in Go (http://golang.org/doc/faq#assertions).

huangapple
  • 本文由 发表于 2015年2月2日 12:41:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/28271223.html
匿名

发表评论

匿名网友

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

确定