lc-go/solutions/11/q1115/solution.go
2026-02-01 14:48:00 +09:00

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{}{}
}
}