WaitOnce is a simple tool that ensures the prerequisites are ready.
go get github.com/plzzzzg/waitonce
package main
import (
"github.com/plzzzzg/waitonce"
"time"
)
func main() {
waitOnceID := "preload"
go func() {
if timeout := waitonce.GetOrCreate(waitOnceID).Wait(time.Second); timeout {
// fallback when timeout
} else {
// do something after preloading done
}
}()
// preload async
go func() {
// preloading
time.Sleep(time.Second)
waitonce.GetOrCreate(waitOnceID).Done()
}()
}
Licensed under the MIT License.