代码之家  ›  专栏  ›  技术社区  ›  Al-geBra

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

  •  0
  • Al-geBra  · 技术社区  · 6 年前

    我正在使用通道和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
    

    为什么要打印内存地址而不是值?我有点困惑。谢谢!

    2 回复  |  直到 6 年前
        1
  •  2
  •   nilsocket    6 年前

    替换此行:

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

    具有

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

    并初始化变量- computation 有价值的 1 factorialViaChannel()

    这样地:

    var computation uint64 = 1 
    
        2
  •  0
  •   Al-geBra    6 年前

    我想出来了,问题已经纠正如下:

    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
    }