package main import ( "fmt" "os" "os/signal" "syscall" "time" ) type Limiter struct { ch chan struct{} } func main() { var l = &Limiter{ ch: make(chan struct{}, 1), } for i := 1; i <= 5; i++ { go l.execute(func() { var fName = "task" fmt.Printf("Starting Execution of %s\n", fName) time.Sleep(time.Second * 5) fmt.Printf("Finished Execution of %s\n", fName) }) } forever() } func forever() { s := make(chan os.Signal, 1) signal.Notify(s, syscall.SIGTERM, syscall.SIGINT) // wait for signal sig := <-s fmt.Printf("\nstarting shutdown procedure, detected signal: %s\n", sig) } func (l *Limiter) execute(f func()) error { l.ch <- struct{}{} defer func() { <-l.ch }() f() return nil }