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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵