package cron12import "time"34type Cron struct {5d time.Duration6ch chan struct{}7}89func NewCron(d time.Duration) *Cron {10return &Cron{11d: d,12ch: make(chan struct{}),13}14}1516func (c *Cron) Do(f func()) {17go func() {18ticker := time.NewTicker(c.d)19defer ticker.Stop()20for {21select {22case <-ticker.C:23f()24case <-c.ch:25return26}27}28}()29}3031func (c *Cron) Stop() {32select {33case _, _ = <-c.ch:34default:35c.ch <- struct{}{}36close(c.ch)37}38}394041