go的context

  • 在context中存储数据
  • 使用context来管理协程

context的本质

1
2
3
4
5
6
type Context interface {
Deadline() (time.Time, bool)
Err() error
value(key any) any
Done() <-chan struct{}
}

emptyContext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// An emptyCtx is never canceled, has no values, and has no deadline. It is not
// struct{}, since vars of this type must have distinct addresses.
type emptyCtx int

func (*emptyCtx) Deadline() (deadline time.Time, ok bool) {
return
}

func (*emptyCtx) Done() <-chan struct{} {
return nil
}

func (*emptyCtx) Err() error {
return nil
}

func (*emptyCtx) Value(key any) any {
return nil
}

go的context
http://example.com/2024/02/29/go的context/
作者
Forrest
发布于
2024年2月29日
许可协议