为有限数量的客户端锁定某些资源的原语称为信号量。
使用缓冲通道很容易实现:
var semaphore = make(chan struct{}, 4) // allow four concurrent usersfunc f() { // Grab the lock. Blocks as long as 4 other invocations of f are still running. semaphore <- struct{}{} // Release the lock once we're done. defer func() { <-semaphore }() // Do work...}


