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