golang 控制方法的访问时间
程序员文章站
2024-01-13 19:30:16
...
package main
import (
"time"
"fmt"
)
func main() {
limitTime(funcA, 6*time.Second)
}
func limitTime(f func(),timer time.Duration) {
ok := make(chan struct{})
go func() {
f()
ok<- struct{}{}
}()
select {
case <- time.After(timer):
fmt.Println("time out")
case <-ok:
}
}
func funcA() {
time.Sleep(time.Second*10)
fmt.Println("there is a function")
}
下一篇: java 设置任务超时时间