Исходники и презентации
This commit is contained in:
26
lessons/contexts/context_with_timeout/main.go
Normal file
26
lessons/contexts/context_with_timeout/main.go
Normal file
@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func makeRequest(ctx context.Context) {
|
||||
timer := time.NewTimer(5 * time.Second)
|
||||
defer timer.Stop()
|
||||
|
||||
select {
|
||||
case <-timer.C:
|
||||
fmt.Println("finished")
|
||||
case <-ctx.Done():
|
||||
fmt.Println("canceled")
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
|
||||
makeRequest(ctx)
|
||||
}
|
||||
Reference in New Issue
Block a user