Skip to content

Instantly share code, notes, and snippets.

@longkey1
Created February 24, 2016 09:04
Show Gist options
  • Save longkey1/44d266833a33436dc8f6 to your computer and use it in GitHub Desktop.
Save longkey1/44d266833a33436dc8f6 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"sync"
"time"
"math/rand"
)
type member struct {
name string
isOchi bool
}
func main() {
var furi sync.WaitGroup
var boke sync.WaitGroup
var act sync.WaitGroup
for _, m := range members() {
if m.isOchi == false {
furi.Add(1)
} else {
boke.Add(1)
}
act.Add(1)
}
for _, m := range members() {
go action(m, furi, boke, act)
}
act.Wait()
}
func action(m member, furi sync.WaitGroup, boke sync.WaitGroup, act sync.WaitGroup) {
defer act.Done()
if false == m.isOchi {
time.Sleep(time.Duration(rand.Intn(5000)) * time.Millisecond)
fmt.Printf("%s: 私がやります!\n", m.name)
furi.Done()
boke.Wait()
fmt.Printf("%s: どうぞどうぞ!\n", m.name)
} else {
furi.Wait()
fmt.Printf("%s: じゃあ、私がやります・・・\n", m.name)
boke.Done()
}
}
func members() []member {
return []member{
member{"higo", false},
member{"jimon", false},
member{"ryuhei", true},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment