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