Not buffered http.ResponseWritter in Golang

As implied in the documentation, some ResponseWriter may implement the Flusher interface.

This means you can do something like this :

func handle(res http.ResponseWriter, req *http.Request) {
  fmt.Fprintf(res, "sending first line of data")
  if f, ok := res.(http.Flusher); ok {
     f.Flush()
  } else {
     log.Println("Damn, no flush");
  }
  sleep(10) //not real code
  fmt.Fprintf(res, "sending second line of data")
}

Be careful that buffering can occur in many other places in the network or client side.

Leave a Comment