0
0

More than 3 years have passed since last update.

眺めて覚えるGo言語 その16 並列処理 「sync.WaitGroupは複数のgoroutineの完了を待つ」

Posted at

眺めて覚えるGo言語 その16 並列処理

「sync.WaitGroupは複数のgoroutineの完了を待つ」

thread.go
package main
import ("fmt";"sync";"time")
func main() {
   wg := sync.WaitGroup{}
   for i := 0; i < 10; i++ {
      wg.Add(1)
      go func (i int) {
         defer wg.Done()
         fmt.Println("Thread no",i)
         time.Sleep(1)
      }(i)
   }
   wg.Wait()
   fmt.Println("complete!")
}

実行結果
>go run thread.go
Thread no 0
Thread no 5
Thread no 2
Thread no 3
Thread no 9
Thread no 4
Thread no 6
Thread no 7
Thread no 8
Thread no 1
complete!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0