package util import ( "sync" ) type Channel struct { m sync.Mutex closed bool C chan interface{} } func NewChannel() *Channel { return &Channel{ C: nil, closed: true, } } func (c *Channel) Open() { c.m.Lock() defer c.m.Unlock() if c.isClosed() { c.C = make(chan interface{}) c.closed = false } } func (c *Channel) Close() { c.m.Lock() defer c.m.Unlock() if !c.isClosed() { close(c.C) c.C = nil c.closed = true } } func (c *Channel) IsClosed() bool { c.m.Lock() defer c.m.Unlock() return c.isClosed() } func (c *Channel) isClosed() bool { return c.C == nil && c.closed }