为有限数量的客户端锁定某些资源的原语称为信号量。它很容易通过缓冲通道实现: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...}