Я работаю с каналами и программами Go, чтобы пойти, чтобы практиковать псевдо-параллелизм.По какой-то причине моя функция факториала, кажется, возвращает адрес, а не фактическое целочисленное значение.Вот мой код:
package main
import (
"fmt"
)
func main() {
c := make(chan uint64)
go factorialViaChannel(8, c)
f := c //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
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
}
Вот мои результаты:
$ go run MultipleConcurrency.go
The Factorial of 8 is 0xc42000c028
The Result of this array multipled computation is 362880
Почему он печатает адрес памяти, а не значение?Я немного смущен.Спасибо!