package main

import (
    "time"
    "context"
    "fmt"
)

func main() {
    d := time.Now().Add(50*time.Millisecond)
    ctx, cancel := context.WithDeadline(context.Background(), d)
    defer cancel()

    select {
    case <-time.After(1*time.Second):
        fmt.Println("overslept")
    case <-ctx.Done():
        fmt.Println(ctx.Err())
    }
}

执行的结果

context deadline exceeded

 

相关文章:

  • 2021-10-16
  • 2021-06-16
  • 2022-12-23
  • 2022-12-23
  • 2021-05-18
  • 2022-02-27
  • 2021-12-10
  • 2021-09-23
猜你喜欢
  • 2022-02-25
  • 2022-12-23
  • 2021-10-28
  • 2022-03-04
  • 2022-12-23
  • 2021-12-28
相关资源
相似解决方案