first commit
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package worker
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ProsesSertifikasi simulates certification processing for a single user.
|
||||
func ProsesSertifikasi(userID int) string {
|
||||
// simulate variable work (sleep)
|
||||
time.Sleep(time.Millisecond * time.Duration(50+userID%10*20))
|
||||
return fmt.Sprintf("user-%d: done", userID)
|
||||
}
|
||||
|
||||
// RunSertifikasi starts n goroutines and waits for all to finish.
|
||||
// Returns results in a slice.
|
||||
func RunSertifikasi(n int) []string {
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(n)
|
||||
|
||||
results := make([]string, n)
|
||||
for i := 0; i < n; i++ {
|
||||
i := i // capture
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
results[i] = ProsesSertifikasi(i + 1)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
return results
|
||||
}
|
||||
Reference in New Issue
Block a user