package timeout import ( "sync" "time" ) type executeChan struct { c chan bool once sync.Once } //Run 返回函数f是否被运行 func Run(limit time.Duration, f func()) bool { ec := &executeChan{c: make(chan bool), once: sync.Once{}} whether := true go func() { f() ec.once.Do(func() { ec.c <- true close(ec.c) }) }() select { case <-time.After(limit): whether = false case <-ec.c: } return whether }