Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Go中的通道,我创建了一个返回地址的阶乘函数_Go_Concurrency - Fatal编程技术网

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

使用Go中的通道,我创建了一个返回地址的阶乘函数,go,concurrency,Go,Concurrency,我正在使用通道和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

我正在使用通道和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
}
为什么打印的是内存地址而不是值?我有点困惑。谢谢

替换此行:

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

替换此行:

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


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

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
}
主程序包
进口(
“fmt”
)
func main(){
阶乘:=make(chan uint64)
去阶乘alviachannel(8,阶乘)

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

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
}
主程序包
进口(
“fmt”
)
func main(){
阶乘:=make(chan uint64)
去阶乘alviachannel(8,阶乘)

f:=快速查看使我想说,请密切注意您对
main
函数中涉及的通道所做的操作。如何从通道
c
获得
factorial
函数的结果?以及如何从通道
product
获得
乘法
函数的结果?您注意到了吗
main
的使用方式不同?@ChronoKitsune你是对的,我知道我做了什么,谢谢!这不是你从频道收到的方式,另一个错误是,
computation
应该设置为
1
,否则你将
0
乘以总是导致
0
的东西@nilsocket红了出来。谢谢!快速看一下,我想说的是,请密切注意您对
main
函数中涉及的通道所做的操作。您如何从通道
c
获得
阶乘
函数的结果?以及通道
产品
乘法
函数的结果如何?你注意到
main
在使用方式上有什么不同吗?@ChronoKitsune你是对的,我知道我做了什么,谢谢!这不是你从频道收到的方式,另一个错误是,
computiation
应该设置为
1
,否则你将
0
乘以总是导致
0
@nilsock的东西是的,我知道了。谢谢!从f频道获取数据:=从f频道获取数据:=
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
}