25 lines
413 B
Go
25 lines
413 B
Go
|
package timeouts
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"math/rand"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func slowMath(a, b int, result chan<- int) {
|
||
|
time.Sleep(time.Duration(rand.Intn(5)) * time.Second)
|
||
|
result <- a + b
|
||
|
}
|
||
|
|
||
|
func Timeouts() {
|
||
|
result := make(chan int, 1)
|
||
|
go slowMath(3, 3, result)
|
||
|
|
||
|
select {
|
||
|
case res := <-result:
|
||
|
fmt.Println("Got result:", res)
|
||
|
case <-time.After(3 * time.Second):
|
||
|
fmt.Println("Waited for 3 second, no result :(")
|
||
|
}
|
||
|
}
|