Skip to content

Instantly share code, notes, and snippets.

@hiyali
Created January 22, 2020 05:19
Show Gist options
  • Save hiyali/3819620bc30b470cec68b782e4aafa34 to your computer and use it in GitHub Desktop.
Save hiyali/3819620bc30b470cec68b782e4aafa34 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"time"
"sync"
)
func work(serverChan chan int, wg *sync.WaitGroup, server int, thing string) {
defer wg.Done()
// processing
time.Sleep(time.Duration(server) * time.Second)
now := time.Now()
fmt.Printf("%v, use server: %d, do thing: %s \n", now, server, thing)
serverChan <- server
}
func main() {
serverCount := 4
serverChan := make(chan int, thingCount)
for i := 1; i <= serverCount; i++ {
serverChan <- i
}
thingCount := 10
for i := 1; i <= thingCount; i++ {
wg.Add(1)
go work(serverChan, &wg, <- serverChan, fmt.Sprintf("thing %d", i))
}
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment