2 回答
TA贡献1816条经验 获得超4个赞
替换这一行:
f := c //Assign go channel value to f
和
f := <-c //Assign go channel value to f
并初始化变量 -computation
值1
在factorialViaChannel()
像这样:
var computation uint64 = 1
TA贡献1789条经验 获得超8个赞
我想通了,问题已在下面更正:
package main
import (
"fmt"
)
func main() {
factorial := make(chan uint64)
go factorialViaChannel(8, factorial)
f := <-factorial //Assign go channel value to f
fmt.Println("The Factorial of 8 is", f)
myNums := []int64{1, 2, 3, 4, 5, 6, 7, 8, 9}
product := make(chan int64)
go multiply(myNums, product) //create go routine pseudo thread
result := <-product
fmt.Println("The Result of this array multipled computation is", result)
}
func factorialViaChannel(value int, factorial chan uint64) {
var computation uint64 = 1
if value < 0 {
fmt.Println("Value can not be less than 0")
} else {
for i := 1; i <= value; i++ {
computation *= uint64(i)
}
}
factorial <- computation
}
func multiply(nums []int64, product chan int64) { //multiply numerous values then send them to a channel
var result int64 = 1
for _, val := range nums {
result *= val
}
product <- result //send result to product
}
- 2 回答
- 0 关注
- 124 浏览
添加回答
举报