A more elegant solution is to use a condition variable. Condition variables provide a simple mechanism which our goroutines can use to signal a change to s.clients. First, we define the condition variable:

There are a couple interesting things in the above code. First of all, notice the locking and unlocking around the call to Wait. It might seem like we're locking for a very long time. But Wait unlocks L on entry and relocks L on exit. This results in much cleaner code -- you lock and unlock normally, without being locked while you wait.

Also, notice that we're still checking our condition inside of a loop. This is because the state of s.clients could be changed by a different goroutine between the time that the signal is sent and our code exiting Wait. (In this specific example, when the blocked goroutine is also the only one that can increment s.clients, the for loop is unecessary. But I wanted to show the for loop example anyways because it's more complete and more common).