36 lines
618 B
Go
36 lines
618 B
Go
package q1115
|
|
|
|
type FooBar struct {
|
|
n int
|
|
foo, bar chan struct{}
|
|
}
|
|
|
|
func NewFooBar(n int) *FooBar {
|
|
ret := &FooBar{
|
|
n: n,
|
|
foo: make(chan struct{}, 1),
|
|
bar: make(chan struct{}, 1),
|
|
}
|
|
ret.bar <- struct{}{}
|
|
return ret
|
|
}
|
|
|
|
func (fb *FooBar) Foo(printFoo func()) {
|
|
for i := 0; i < fb.n; i++ {
|
|
<-fb.bar
|
|
// printFoo() outputs "foo". Do not change or remove this line.
|
|
printFoo()
|
|
|
|
fb.foo <- struct{}{}
|
|
}
|
|
}
|
|
|
|
func (fb *FooBar) Bar(printBar func()) {
|
|
for i := 0; i < fb.n; i++ {
|
|
<-fb.foo
|
|
// printBar() outputs "bar". Do not change or remove this line.
|
|
printBar()
|
|
|
|
fb.bar <- struct{}{}
|
|
}
|
|
}
|