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条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大