0

Reading The Go Memory Model, I fell on this code snippet.

var limit = make(chan int, 3)

func main() {
    for _, w := range work {
        go func(w func()) {
            limit <- 1
            w()
            <-limit
        }(w)
    }
    select{}
}

I understand what this function is supposed to do – limit concurrency to 3 goroutines at any time – but I don't understand what the final select{} does. I expect this is some way to keep main alive until all goroutines have finished running, but I can't really say for sure.

What happens in an empty select?

LodeRunner
  • 7,975
  • 3
  • 22
  • 24
  • 3
    Check out this question: [Go project's main goroutine sleep forever?](https://stackoverflow.com/questions/36419054/go-projects-main-goroutine-sleep-forever/36419222#36419222) – icza Jul 04 '17 at 15:49

1 Answers1

2

In generally, select{} is used for infinite loop.

mattn
  • 7,571
  • 30
  • 54