继续默认为什么其他案例没有被打印出来

当我运行此代码时,我不明白为什么在打印X后它不打印其他情况。我希望我应该看到X打印出来三秒钟,然后最后从server2


package main



func server1(ch chan string) {

    time.Sleep(6 * time.Second)

    ch <- "from server1"

}

func server2(ch chan string) {

    time.Sleep(3 * time.Second)

    ch <- "from server2"


}

func main() {

    output1 := make(chan string)

    output2 := make(chan string)

    go server1(output1)

    go server2(output2)

    select {

    case s1 := <-output1:

        fmt.Println(s1)

    case s2 := <-output2:

        fmt.Println(s2)

    default:

        fmt.Println(“X”)

    }

}


繁星点点滴滴
浏览 55回答 1
1回答

qq_笑_17

func main() {&nbsp; &nbsp; output1 := make(chan string)&nbsp; &nbsp; output2 := make(chan string)&nbsp; &nbsp; go server1(output1)&nbsp; &nbsp; go server2(output2)&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; select {&nbsp; &nbsp; &nbsp; &nbsp; case s1 := <-output1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(s1)&nbsp; &nbsp; &nbsp; &nbsp; case s2 := <-output2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(s2)&nbsp; &nbsp; &nbsp; &nbsp; default: // todo: return&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("X")&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go