From 77aec8134e4e801383ac33ceb5549d40f301edb5 Mon Sep 17 00:00:00 2001 From: "fuweihu.fwh" Date: Mon, 9 Jan 2023 20:17:47 +0800 Subject: [PATCH] . --- reuse/golang/src/package/context/Makefile | 4 +++ .../src/package/context/deadline_example.go | 27 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 reuse/golang/src/package/context/Makefile create mode 100644 reuse/golang/src/package/context/deadline_example.go diff --git a/reuse/golang/src/package/context/Makefile b/reuse/golang/src/package/context/Makefile new file mode 100644 index 0000000..1216cd0 --- /dev/null +++ b/reuse/golang/src/package/context/Makefile @@ -0,0 +1,4 @@ + + +run_deadline_example: + go run deadline_example.go diff --git a/reuse/golang/src/package/context/deadline_example.go b/reuse/golang/src/package/context/deadline_example.go new file mode 100644 index 0000000..d4e95e1 --- /dev/null +++ b/reuse/golang/src/package/context/deadline_example.go @@ -0,0 +1,27 @@ +package main + +import ( + "context" + "fmt" + "time" +) + +const shortDuration = 1 * time.Millisecond + +func main() { + d := time.Now().Add(shortDuration) + ctx, cancel := context.WithDeadline(context.Background(), d) + fmt.Println(ctx.Deadline()) + + defer cancel() + select { + case <-time.After(1 * time.Second): + fmt.Println("overslept") + case <-ctx.Done(): + fmt.Println(ctx.Err()) + } +} + +//go run deadline_example.go +//2023-01-09 20:06:23.233551 +0800 CST m=+0.001400468 true +//context deadline exceeded