donglu5000 2011-11-22 15:46
浏览 16
已采纳

去编程语言代码错误

I try to write a simple code in GO, in which two go routine (Send and Receive) send each other integers. I give the code below. Can anybody help me why the output of this program is [no output]. Is there any silly mistake (sorry, I am new in GO) ?

package main

func Send (in1 <-chan int, out1 chan<- int) {

                i := 2 
        out1 <- i 
    print(i, "
")
}

func Receive (in <-chan int, out chan<- int) {

        i := <-in 
        print(i, "
")
        out <- i 
}


func main() {

       for i := 0; i < 10; i++ {
        ch1 := make(chan int)
        ch := make(chan int) 
            go Send (ch1 , ch)
        go Receive (ch , ch1)
        ch = ch1
        ch1 = ch
        }

}
  • 写回答

2条回答 默认 最新

  • donglian2106 2011-11-22 16:13
    关注

    How about this:

    package main
    
    func Send (ch chan<- int) {
        for i := 0; i < 10; i++ {
          print(i, " sending
    ")
          ch <- i
        }
    }
    
    func Receive (ch <-chan int) {
        for i := 0; i < 10; i++ {
            print(<-ch, " received
    ")
        }
    }
    
    func main() {
       ch := make(chan int)
       go Receive(ch)
       Send(ch)
    }
    

    The output of this when I run it on golang.org is:

    0 sending
    0 received
    1 sending
    2 sending
    1 received
    2 received
    3 sending
    4 sending
    3 received
    4 received
    5 sending
    6 sending
    5 received
    6 received
    7 sending
    8 sending
    7 received
    8 received
    9 sending
    

    I'm not sure why the 9 was never received. There should be some way to sleep the main thread until the Receive goroutine has completed. Also, it's inelegant that the receiver and the sender both know that they are going to send 10 numbers. One of the goroutines should just shut off when the other has finished its work. I'm not sure how to do that.

    EDIT1:

    Here is an implementation with two channels, and the go routines send ints back and forth between eachother. One is designated as the responder, who only sends and int after it receives an int. The responder simply adds two to the int he receives, and then sends it back.

    package main
    
    func Commander(commands chan int, responses chan int) {
        for i := 0; i < 10; i++ {
          print(i, " command
    ")
          commands <- i
          print(<-responses, " response
    ");
        }
        close(commands)
    }
    
    func Responder(commands chan int, responses chan int) {
        for {
            x, open := <-commands
            if !open {
                return;
            }
            responses <- x + 2
        }
    }
    
    func main() {
       commands := make(chan int)
       responses := make(chan int)
       go Commander(commands, responses)
       Responder(commands, responses)
    }
    

    The output when I run it on golang.org is:

    0 command
    2 response
    1 command
    3 response
    2 command
    4 response
    3 command
    5 response
    4 command
    6 response
    5 command
    7 response
    6 command
    8 response
    7 command
    9 response
    8 command
    10 response
    9 command
    11 response
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • duanqiang2977 2011-11-22 16:35
    关注

    David Grayson's fix works, but the explanation of why 9 is not always received wouldn't fit into a comment!

    When the sending goroutine sends the value on the channel, it blocks until the receiving goroutine receives it. At that point, it's unblocked. The go scheduler might return from main almost immediately before it gives the receiving goroutine a chance to print anything. If you set GOMAXPROCS to something greater than 1, this happens less frequently because the receiving goroutine won't block while the main goroutine is active.

    If you want to stop main from returning until all goroutines are finished, you can return a channel from Receive(), like this:

    package main
    
    import "fmt"
    
    func Send(ch chan<- int) {
        for i := 0; i < 10; i++ {
            fmt.Println(i, " sending")
            ch <- i
        }
        close(ch)
    }
    
    func Receive(ch <-chan int) (<-chan bool) {
        done := make(chan bool)
        go func() {
            for {
                i, ok := <-ch
                if !ok {
                    break
                }
                fmt.Println(i, " received")
            }
            done <- true
        }()
        return done
    }
    
    func main() {
        ch := make(chan int)
        d := Receive(ch)
        Send(ch)
        _ = <-d
    }
    

    As for why your output was empty in your original example: you make two goroutines, and thus main is totally unblocked, so it just returns and the program exits! I've definitely made this mistake too, but the runtime only joins with the main goroutine, not with ALL goroutines, like you may expect.

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 QT 实现 RSTP 语音对讲功能
  • ¥50 AES魔改之后的安全性关于PRF(相关搜索:密码学)
  • ¥15 有没有谁能高分通过 reCaptcha v3验证,国外网站。有兴趣留言,有偿。
  • ¥15 用C语言写的一个程序遇到了两个问题第一是偏移正确但读取不到坐标,第二个问题是自己定义的函数实现不了获取指定进程模块。
  • ¥15 在安装Anaconda时总是闪退怎么办?
  • ¥15 对图中电路进行以下几个方面的分析
  • ¥15 对图中电路进行以下几个方面的分析
  • ¥15 对图中电路进行以下几个方面的分析
  • ¥15 对图中电路进行以下几个方面的分析
  • ¥500 抖音主页视频预存加载卡bug