英文:
Is http.CloseNotify entirely replaced by http.Request.Context().Done()?
问题
Go 1.7在http.Request中添加了Context。它是否完全替代了http.CloseNotify?现在我应该优先使用Context,而不用关心CloseNotify吗?
英文:
Go 1.7 added Context to the http.Request. Does it completely replace http.CloseNotify? Should I prefer that now, and not bother with CloseNotify?
答案1
得分: 23
是的,但只适用于Go 1.8及以上版本。在Go 1.7中存在这些API,但直到Go 1.8中,客户端的断开连接才会导致请求的上下文(Context)被标记为完成(Done),因此在Go 1.7中仍然存在有效使用CloseNotifier的情况。
(来源:我向标准库添加了"context"并且是net/http包的作者。)
英文:
Yes, but only in Go 1.8+. The APIs existed in Go 1.7 but it wasn't until Go 1.8 that the client's disconnection caused the Request's Context to be Done, so in Go 1.7 there was still a valid use of CloseNotifier.
(Source: I added "context" to the standard library and am the author of the net/http package.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论