Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
daidai21 committed Jan 9, 2023
1 parent c18f61a commit 77aec81
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions reuse/golang/src/package/context/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@


run_deadline_example:
go run deadline_example.go
27 changes: 27 additions & 0 deletions reuse/golang/src/package/context/deadline_example.go
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 77aec81

Please sign in to comment.