github.com/nibnait/go-learn@v0.0.0-20220227013611-dfa47ea6d2da/src/test/chapter/ch4/32_first_response_test.go (about) 1 package ch4 2 3 import ( 4 "fmt" 5 "runtime" 6 "testing" 7 "time" 8 ) 9 10 func runTask1(id int) string { 11 time.Sleep(10 * time.Millisecond) 12 return fmt.Sprintf("The result is from %d", id) 13 } 14 15 func FirstResponse() string { 16 numOfRunner := 10 17 ch := make(chan string, numOfRunner) 18 19 for i := 0; i < numOfRunner; i++ { 20 go func(i int) { 21 ret := runTask1(i) 22 ch <- ret 23 }(i) 24 } 25 26 return <-ch 27 } 28 29 func TestFirstResponse(t *testing.T) { 30 t.Log("Before:", runtime.NumGoroutine()) 31 t.Log(FirstResponse()) 32 time.Sleep(time.Second * 1) 33 t.Log("After:", runtime.NumGoroutine()) 34 }