A workonce worker is useful when many goroutines need to perform the same job that is expected to have the same result for all of them. Instead of all the routines executing the work, only one routine is allowed to perform the work while the rest of the routines wait for the result.
package main
import (
"github.com/minitauros/go-workonce"
)
type WorkResult struct {
}
func main() {
// Create a work worker that identifies the work uniquely by string and
// that runs work that returns a WorkResult.
worker := workonce.NewWorker[string, WorkResult]()
// Run the unique piece of work "refresh-login-token". If the work called
// "refresh-login-token" is already being executed by another goroutine, the
// worker will return the WorkResult of the already active routine once it
// finishes.
res, _ := worker.Run("refresh-login-token", func() (WorkResult, error) {
return WorkResult{}, nil
})
// Do something with res.
doSomethingWith(res)
}