1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
func RunWithTimeout(fun func() error, timeout time.Duration) error {
finished := make(chan struct{})
var err error
go func() {
err = fun()
finished <- struct{}{}
}()
select {
case <-finished:
return err
case <-time.After(timeout):
return fmt.Errorf("timeout")
}
}
|