doujiu9172 2018-09-13 19:56
浏览 17
已采纳

在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!

  • 写回答

2条回答 默认 最新

  • dongtu1357 2018-09-13 21:03
    关注

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 python进程启动打包问题
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题