Skip to content

Instantly share code, notes, and snippets.

@siadat
Last active October 19, 2017 07:03
Show Gist options
  • Select an option

  • Save siadat/0e18be6f246a58f879cdcedb5fc7cf10 to your computer and use it in GitHub Desktop.

Select an option

Save siadat/0e18be6f246a58f879cdcedb5fc7cf10 to your computer and use it in GitHub Desktop.
Chunked Response
package main
import (
"fmt"
"log"
"net/http"
"time"
)
// Test with:
// curl -N localhost:9090
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
flusher, ok := w.(http.Flusher)
if !ok {
w.WriteHeader(http.StatusInternalServerError)
return
}
for i := 0; i < 10; i++ {
time.Sleep(200 * time.Millisecond)
w.Write([]byte(fmt.Sprintf("chunk %d", i)))
flusher.Flush()
}
})
log.Println("starting on :9090")
log.Fatal(http.ListenAndServe(":9090", nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment