Skip to content
Go

goroutine

goroutine で並行処理を実装。

#goroutine#concurrency

Code

go
package main

import (
    "fmt"
    "sync"
    "time"
)

func main() {
    var wg sync.WaitGroup
    for i := 0; i < 5; i++ {
        wg.Add(1)
        go func(n int) {
            defer wg.Done()
            time.Sleep(time.Second)
            fmt.Println("goroutine", n)
        }(i)
    }
    wg.Wait()
    fmt.Println("done")
}