1. 只运行一次


package once_testimport ( "fmt" "sync" "testing" "unsafe")type Singleton struct {}var singleInstance *Singletonvar once sync.Oncefunc GetSingletonObj() *Singleton { once.Do(func() { fmt.Println("Create obj") singleInstance = new(Singleton) }) return singleInstance}func TestGetSingletonObj(t *testing.T) { var wg sync.WaitGroup for i := 0; i < 10; i++ { wg.Add(1) go func() { obj := GetSingletonObj() fmt.Printf("%x\n", unsafe.Pointer(obj)) wg.Done() }() } wg.Wait()}
2. 仅需任意任务完成
package concurrencyimport ( "fmt" "runtime" "testing" "time")func runTask(id int) string { time.Sleep(10 * time.Millisecond) return fmt.Sprintf("The result is from %d", id)}func FirstResponse() string { numOfRunner := 10 //采用buffer channel 来防止协程泄露 ch := make(chan string, numOfRunner) //ch := make(chan string) for i := 0; i < numOfRunner; i++ { go func(i int) { ret := runTask(i) ch <- ret }(i) } return <-ch}func TestFirstResponse(t *testing.T) { t.Log("Before:", runtime.NumGoroutine()) t.Log(FirstResponse()) time.Sleep(time.Second * 1) t.Log("After:", runtime.NumGoroutine())}
3. 所有任务完成
package util_all_doneimport ( "fmt" "runtime" "testing" "time")func runTask(id int) string { time.Sleep(10 * time.Millisecond) return fmt.Sprintf("The result is from %d", id)}func FirstResponse() string { numOfRunner := 10 ch := make(chan string, numOfRunner) for i := 0; i < numOfRunner; i++ { go func(i int) { ret := runTask(i) ch <- ret }(i) } return <-ch}func AllResponse() string { numOfRunner := 10 ch := make(chan string, numOfRunner) for i := 0; i < numOfRunner; i++ { go func(i int) { ret := runTask(i) ch <- ret }(i) } finalRet := "" for j := 0; j < numOfRunner; j++ { finalRet += <-ch + "\n" } return finalRet}func TestFirstResponse(t *testing.T) { t.Log("Before:", runtime.NumGoroutine()) t.Log(AllResponse()) time.Sleep(time.Second * 1) t.Log("After:", runtime.NumGoroutine())}
4. 对象池
5. sync.pool对象缓存




