在这个函数中,你可以看到我使用了 go 关键字。
package main
import (
"fmt"
"math"
)
func main() {
c := make(chan string)
go findGreatestDivisor(4, c)
for i := 0; i <= 1; i++ {
fmt.Println(<-c)
}
}
func findGreatestDivisor(num float64, c chan string) {
var counter float64 = 10000
for i := 9999; i > 0; i-- {
if math.Mod(num, counter) == 0 {
fmt.Println("Check..", math.Mod(4, 1))
c <- fmt.Sprintf("%f is divisble by %d", num, i)
}
counter--
}
}
有用。它给了我最大的整数。但是现在我很好奇并删除了我在此处调用该函数的 go 关键字
go findGreatestDivisor(4,c)
当我只是做
findGreatestDivisor(4,c)
它给了我错误
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan send]: main.findGreatestDivisor(0x4010000000000000,
0xc82001a0c0)
/home/ubuntu/workspace/test.go:21 +0x37c main.main()
/home/ubuntu/workspace/test.go:10 +0x61 exit status 2
这是为什么?
梦里花落0921
相关分类