Работая с каналами в Go, я создал факториальную функцию, которая возвращает адрес - PullRequest
0 голосов
/ 13 сентября 2018

Я работаю с каналами и программами 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 ]

0 голосов
/ 14 сентября 2018

Я разобрался, проблема была исправлена ​​ниже:

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
}
0 голосов
/ 13 сентября 2018

Заменить эту строку:

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

с

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

, а также инициализировать переменную - computation со значением 1 в factorialViaChannel()

как это:

var computation uint64 = 1 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...