Utility designed for concurrent handling.
To get the package, execute:
go get github.com/gofika/fikasync
package main
import (
"log"
"time"
"github.com/gofika/fikasync"
)
// fakeTask simulates a task that takes some time to complete.
func fakeTask(i int) {
// your task code here...
log.Printf("Executing task %d\n", i)
// simulate some work
time.Sleep(time.Second)
}
func main() {
// create a new limiter that allows 5 concurrent operations
limiter := fikasync.NewConcurrentLimiter(5)
defer limiter.Close()
// run 20 tasks
for i := 0; i < 20; i++ {
// run the task in a goroutine, blocking if the maximum number of concurrent operations has been reached
limiter.Go(func() {
fakeTask(i)
})
}
limiter.Wait()
log.Println("All tasks completed")
}