去通道不输出它传递的数据

我是新手,我正在尝试频道并发现了这一点。


func main() {

    c := make(chan int)

    fmt.Println("initialized channel")

    go receiver(c)

    go helper(c)


    for x := range c {

        fmt.Println(x)

    }

}


func helper(c chan int) {

    time.Sleep(time.Second * 3)

    c <- 5

    time.Sleep(time.Second * 3)

    c <- 4

    close(c)

}


func receiver(c chan int) {

    for x := range c {

        fmt.Println(x)

    }

}


问题是即使我发送了两个号码,但控制台中只打印了一个号码。


initialized channel

5

输出


喵喵时光机
浏览 83回答 1
1回答

慕码人2483693

行为无法预测。有时您的程序会工作,有时则不会。我已经解释了问题发生的代码(带有注释)。package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "time")func main() {&nbsp; &nbsp; c := make(chan int)&nbsp; &nbsp; fmt.Println("initialized channel")&nbsp; &nbsp; // This is a receiver receiving from c (spawned goroutine)&nbsp; &nbsp; go receiver(c)&nbsp; &nbsp; // This is a sender (spawned goroutine)&nbsp; &nbsp; go helper(c)&nbsp; &nbsp; // This is again a receiver receiving from c&nbsp; &nbsp; // NOTE: As reciever and helper are spawned in&nbsp; &nbsp; // separate goroutine, control eventually reaches&nbsp; &nbsp; // here.&nbsp; &nbsp; // Behaviour is unpredictable as sometimes the data&nbsp; &nbsp; // sent to the channel might be recieved here and&nbsp; &nbsp; // sometimes it might be recieved by the receiver function.&nbsp; &nbsp; for x := range c {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(x)&nbsp; &nbsp; }}func helper(c chan int) {&nbsp; &nbsp; time.Sleep(time.Second * 3)&nbsp; &nbsp; c <- 5&nbsp; &nbsp; time.Sleep(time.Second * 3)&nbsp; &nbsp; c <- 4&nbsp; &nbsp; // When this close is triggered, the receiver in main&nbsp; &nbsp; // could get exited as the signal to stop ranging is sent&nbsp; &nbsp; // using signal. Right after that the main function ends&nbsp; &nbsp; // such that data recieved by the receiver couldn't get&nbsp; &nbsp; // printed (sometimes it would work as well) i.e., main&nbsp; &nbsp; // exited right before fmt.Println(x) in receiver function.&nbsp; &nbsp; close(c)}func receiver(c chan int) {&nbsp; &nbsp; for x := range c {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(x)&nbsp; &nbsp; }}要修复它,你可以试试这个。还有更多可能的解决方案,但这已经足够了。我已经删除了time.Sleep电话,因为它们与我们无关并且为简洁起见。package mainimport (&nbsp; &nbsp; "fmt")func main() {&nbsp; &nbsp; // Initialize the channel&nbsp; &nbsp; c := make(chan int)&nbsp; &nbsp; // Spawn a goroutine that sends data to the&nbsp; &nbsp; // channel. Also, it is expected from the sender&nbsp; &nbsp; // only to close the channel as it only knows&nbsp; &nbsp; // when then send stops. Send to a closed&nbsp; &nbsp; // channel would panic.&nbsp; &nbsp; go send(c)&nbsp; &nbsp; // As send is running asynchronously, control&nbsp; &nbsp; // reaches here i.e., the receiver. It ranges until&nbsp; &nbsp; // c is closed and is guaranteed to receive every&nbsp; &nbsp; // date sent to channel c and then exit.&nbsp; &nbsp; for x := range c {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(x)&nbsp; &nbsp; }}// send data to cfunc send(c chan int) {&nbsp; &nbsp; c <- 5&nbsp; &nbsp; c <- 4&nbsp; &nbsp; close(c)}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go