我在我的应用程序中使用 CloseNotifier,代码如下所示
func Handler(res http.ResonseWriter, req *http.Request) {
notify := res.(CloseNotifier).CloseNotify()
someLogic();
select {
case <-notify:
someCleanup()
return;
default:
}
someOtherLogic();
}
我注意到 CloseNotifier 现在已被弃用。 From source code :
// Deprecated: the CloseNotifier interface predates Go's context package.
// New code should use Request.Context instead.
但是,我不确定如何在此处使用 Request.Context。
最佳答案
实际上看起来很简单。 From this blogpost :
func Handler(res http.ResonseWriter, req *http.Request) {
ctx := req.Context()
someLogic();
select {
case <-ctx.Done():
someCleanup(ctx.Err())
return;
default:
}
someOtherLogic();
}
关于go - 如何使用 request.Context 而不是 CloseNotifier?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54890809/