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

报告相同问题?

悬赏问题

  • ¥15 C++ 如何判断设置快捷键来实现隐藏/显示窗口
  • ¥15 关于#材料工程#的问题:有没有具有电子阻挡层和空穴阻挡层的电池仿真silvaco代码例子或者其他器件具有阻挡层例子的silvaco代码(最好还有相关文献)
  • ¥60 基于MATLAB的TAOD算法
  • ¥15 Groimp使用疑问
  • ¥15 MDK–ARM里一直找不到调试器
  • ¥15 oracle中sql查询问题
  • ¥15 vue使用gojs3.0版本,在nodeDataArray中的iconSrc使用gif本地路径,展示出来后动画是静态的,不是动态的
  • ¥100 代写个MATLAB代码,有偿
  • ¥15 ansys electronics 2021 R1安装报错,错误代码2,如图
  • ¥15 Dev-c++打字不出现中文,但出现日文