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 来一个cc穿盾脚本开发者
  • ¥15 CST2023安装报错
  • ¥15 使用diffusionbert生成文字 结果是PAD和UNK怎么办
  • ¥15 有人懂怎么做大模型的客服系统吗?卡住了卡住了
  • ¥20 firefly-rk3399上启动卡住了
  • ¥15 如何删除这个虚拟音频
  • ¥50 hyper默认的default switch
  • ¥15 网站打不开,提示502 Bad Gateway
  • ¥20 基于MATLAB的绝热压缩空气储能系统代码咨询
  • ¥15 R语言建立随机森林模型出现的问题