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 關注
- 161 瀏覽
添加回答
舉報