golang,大括号或 return 周围的奇怪错误
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; ; /* infinite loop */ i++ {
// your code goes here
if isPrimeNumber(i){
chan <- i <========error on this line
}
assert(i < 100) // i is afraid of heights
}
}
我在这方面遇到了错误,但无法弄清楚,需要帮助。谢谢
语法错误:意外的分号或换行符,需要{
失败
使用 channel <- i
而不是 chan <- i
。
在你的函数定义(channel chan int
)中,channel
是参数名,chan int
是类型。澄清一下,您的函数可以重写为以下函数:
func findPrimeNumbers(primeNumberChannel chan int) {
for i := 2; ; i++ {
if isPrimeNumber(i){
primeNumberChannel <- i
}
}
}
此外,assert
在 Go (http://golang.org/doc/faq#assertions) 中不可用。
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; ; /* infinite loop */ i++ {
// your code goes here
if isPrimeNumber(i){
chan <- i <========error on this line
}
assert(i < 100) // i is afraid of heights
}
}
我在这方面遇到了错误,但无法弄清楚,需要帮助。谢谢
语法错误:意外的分号或换行符,需要{ 失败
使用 channel <- i
而不是 chan <- i
。
在你的函数定义(channel chan int
)中,channel
是参数名,chan int
是类型。澄清一下,您的函数可以重写为以下函数:
func findPrimeNumbers(primeNumberChannel chan int) {
for i := 2; ; i++ {
if isPrimeNumber(i){
primeNumberChannel <- i
}
}
}
此外,assert
在 Go (http://golang.org/doc/faq#assertions) 中不可用。