英文:
CloseNotifier for http ResponseWriter
问题
我正在使用Go 1.4.2版本,但是实现中似乎没有CloseNotifier
,我想在长轮询处理程序中使用它,类似于:
func Pollhandler(w http.ResponseWriter, r *http.Request) {
notify := w.(CloseNotifier).CloseNotify()
<-notify //应该阻塞,直到HTTP连接关闭
}
CloseNotifier
在http.ResponseWriter
中没有实现吗?如果是这样,我该如何解决这个问题?或者是否有任何实现了CloseNotifier
接口的http.ResponseWriter
的实现?
英文:
i am using go 1.4.2 and the implementation doesn't seem to have CloseNotifier
as i want to use it in a long polling handler with something like:
func Pollhandler(w http.ResponseWriter, r *http.Request) {
notify := w.(CloseNotifier).CloseNotify()
<-notify //should block until the http connection is closed
}
is the CloseNotifier not implemented for the http ResponseWriter
? if so how can i get around this? or is there any implementation for http ResponseWriter
that implements the CloseNotifier
interface?
答案1
得分: 4
http.CloseNotifier
自Go 1.1版本以来就存在了(http://golang.org/doc/go1.1)。你的代码不起作用是因为你忘记了包的部分:
notify := w.(http.CloseNotifier).CloseNotify()
英文:
http.CloseNotifier
has been there since Go 1.1. Your code doesn't work because you forgot the package part:
notify := w.(http.CloseNotifier).CloseNotify()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论