github.com/sandwich-go/boost@v1.3.29/singleflight/README.md (about)

     1  # singleflight
     2  
     3  重复功能调用抑制
     4  
     5  # 例子
     6  
     7  ```go
     8  var do1 = func() (interface{}, error) {
     9      time.Sleep(100 * time.Millisecond)
    10      return 1, nil
    11  }
    12  var do2 = func() (interface{}, error) {
    13      time.Sleep(100 * time.Millisecond)
    14      return 2, nil
    15  }
    16  
    17  var key = "key"
    18  var g = New()
    19  var wg sync.WaitGroup
    20  wg.Add(2)
    21  var ret1, ret2 interface{}
    22  go func() {
    23      ret1, _ = g.Do(key, do1)
    24      wg.Done()
    25  }()
    26  go func() {
    27      ret2, _ = g.Do(key, do2)
    28      wg.Done()
    29  }()
    30  wg.Wait()
    31  
    32  fmt.Println(ret1 == ret2)
    33  ```
    34  
    35  Output:
    36  ```text
    37  true
    38  ```