Files
deep_go/lessons/contexts/context_with_timeout/main.go

27 lines
381 B
Go

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)
}