awesome-patterns/master_concurrent_go/ch01/main.go

39 lines
511 B
Go
Raw Normal View History

2018-04-27 15:29:19 +03:00
package main
import (
"fmt"
2018-04-27 15:58:19 +03:00
"sync"
2018-04-27 15:29:19 +03:00
"time"
)
type Job struct {
i int
max int
text string
}
2018-04-27 15:58:19 +03:00
func outputText(j *Job, wg *sync.WaitGroup) {
defer wg.Done()
2018-04-27 15:29:19 +03:00
for j.i < j.max {
time.Sleep(1 * time.Millisecond)
fmt.Println(j.text)
j.i++
}
}
func main() {
2018-04-27 15:58:19 +03:00
wg := new(sync.WaitGroup)
2018-04-27 15:29:19 +03:00
hello := new(Job)
world := new(Job)
hello.text = "hello"
hello.i = 0
hello.max = 3
world.text = "world"
world.i = 0
world.max = 5
2018-04-27 15:58:19 +03:00
go outputText(hello, wg)
go outputText(world, wg)
wg.Add(2)
wg.Wait()
2018-04-27 15:29:19 +03:00
}