go中的非阻塞通道

我有一些这样的代码:


go func(ch chan bool) chan bool {


    // some processing

    ch <- true

    return ch

}(ch)


for i := 0; i < TotalQuestions; i++ {

    // check if channel ch has some value

    // If it has then execute below statements, else break out of the loop



    fmt.Println(questions[i])

    answer, _ := InputReader.ReadString('\n')


    // some processing


}

fmt.Println("Your Total score is " + strconv.Itoa(TotalScore) + " out of " + strconv.Itoa(TotalQuestions))

现在我要做的是检查通道 ch 是否有值(在 for 循环中)。如果它有一个值,那么我想跳出 for 循环来打印最后一条语句。否则,我想继续我的循环。我试图插入选择块,但没有用(通道被阻塞,代码没有打印问题)。怎么做?


牛魔王的故事
浏览 86回答 2
2回答

犯罪嫌疑人X

package main&nbsp; &nbsp;&nbsp;import (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "log"&nbsp; &nbsp; "math/rand"&nbsp; &nbsp; "time")func main() {&nbsp; &nbsp; // user score, no.of questions asked so far&nbsp; &nbsp; var score, num int&nbsp; &nbsp; var correct bool // temporary variable to decide if the answer is right&nbsp; &nbsp; // questions&nbsp; &nbsp; var questions = make([]string, 13)&nbsp; &nbsp; t1 := time.Tick(time.Second * 7) // timer loop:&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; select {&nbsp; &nbsp; &nbsp; &nbsp; case <-t1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Println("ran out of time")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break loop&nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // have any questions further questions to ask&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if num < len(questions) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // simulate typing&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time.Sleep(time.Millisecond * 777)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // correct or wrong answer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; correct = (rand.Intn(777)%2 == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if correct {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("you did it")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; score++ //increase score&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("try again")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // no questions, state and break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Println("all questions were finished")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break loop //break loop, all questions were finished&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num++&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; //print final score&nbsp; &nbsp; fmt.Println("your score is:", score)}

翻翻过去那场雪

以非阻塞方式从通道读取的功能方式:func CollectChanOne[T any](ch <-chan T) (T, bool) {&nbsp; &nbsp; select {&nbsp; &nbsp; case val, stillOpen := <-ch:&nbsp; &nbsp; &nbsp; &nbsp; return val, stillOpen&nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; var zeroT T&nbsp; &nbsp; &nbsp; &nbsp; return zeroT, false&nbsp; &nbsp; }}示例: https: //go.dev/play/p/Njwyt32B4oT注意此示例还有另一种方法 CollectChanRemaining() ,它读取通道中的所有缓冲元素。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go