在Go中使用通道,我创建了一个返回地址的阶乘函数

I am working with Channels and Go Routines to Go to practice pseudo-Concurrency. For some reason, my Factorial function appears to be returning an address as opposed to an actual integer value. Here is my code:

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
}

Here are my results:

$ go run MultipleConcurrency.go
The Factorial of 8 is 0xc42000c028
The Result of this array multipled computation is 362880

Why is it printing a memory address as opposed to a value? I'm a bit confused. Thanks!

I figured it out, the issue has been corrected below:

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
}

Replace this line:

f := c //Assign go channel value to f

with

f := <-c //Assign go channel value to f

and also initialize variable - computation with value 1 in factorialViaChannel()

like this:

var computation uint64 = 1