Why is this Go code blocking?

后端 未结 3 1440
旧巷少年郎
旧巷少年郎 2020-12-05 12:04

I wrote the following program:

package main

import (
    \"fmt\"
)

func processevents(list chan func()) {
    for {
        //a := <-list
        //a()
         


        
3条回答
  •  Happy的楠姐
    2020-12-05 12:39

    Here is another solution - use range to read from the channel. This code will yield to the scheduler correctly and also terminate properly when the channel is closed.

    func processevents(list chan func()) {
        for a := range list{
            a()
        }
    }
    

提交回复
热议问题