Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go context 相关问题 #1

Open
storyflow opened this issue Nov 17, 2021 · 1 comment
Open

Go context 相关问题 #1

storyflow opened this issue Nov 17, 2021 · 1 comment
Labels

Comments

@storyflow
Copy link
Owner

storyflow commented Nov 17, 2021

1、context结构是什么样的?
2、context使用场景和用途?

@storyflow
Copy link
Owner Author

Go context的结构

  • Deadline — 返回 context.Context 被取消的时间,也就是完成工作的截止日期;
  • Done — 返回一个 Channel,这个 Channel 会在当前工作完成或者上下文被取消后关闭,多次调用 Done 方法会返回同一个 Channel;
  • Err — 返回 context.Context 结束的原因,它只会在 Done 方法对应的 Channel 关闭时返回非空的值;
    如果 context.Context 被取消,会返回 Canceled 错误;
    如果 context.Context 超时,会返回 DeadlineExceeded 错误;
  • Value — 从 context.Context 中获取键对应的值,对于同一个上下文来说,多次调用 Value 并传入相同的 Key 会返回相同的结果,该方法可以用来传递请求特定的数据;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant