http.ResponseWriter的Flush

func handle(res http.ResponseWriter, req *http.Request) {
  fmt.Fprintf(res, "sending first line of data")
  sleep(10) 
  fmt.Fprintf(res, "sending second line of data")
}
这样first data和second data都会最后同时传到客户端浏览器。但是我们的目的是为了能一有数据就先传给浏览器。所以:
func handle(res http.ResponseWriter, req *http.Request) {
  fmt.Fprintf(res, "sending first line of data")
  res.(http.Flusher).Flush()
  sleep(10)
  fmt.Fprintf(res, "sending second line of data")
}

资料:
https://stackoverflow.com/questions/19292113/not-buffered-http-responsewritter-in-golang
posted @ 2017-06-16 14:51  小鼬就是我  阅读(2318)  评论(0编辑  收藏  举报