main.go 536 B

12345678910111213141516171819202122232425262728293031323334
  1. package main
  2. import (
  3. "fmt"
  4. "sync"
  5. "time"
  6. "github.com/tal-tech/go-zero/core/stringx"
  7. "github.com/tal-tech/go-zero/core/syncx"
  8. )
  9. func main() {
  10. const round = 5
  11. var wg sync.WaitGroup
  12. barrier := syncx.NewSharedCalls()
  13. wg.Add(round)
  14. for i := 0; i < round; i++ {
  15. go func() {
  16. defer wg.Done()
  17. val, err := barrier.Do("once", func() (interface{}, error) {
  18. time.Sleep(time.Second)
  19. return stringx.RandId(), nil
  20. })
  21. if err != nil {
  22. fmt.Println(err)
  23. } else {
  24. fmt.Println(val)
  25. }
  26. }()
  27. }
  28. wg.Wait()
  29. }