Skip to content

Instantly share code, notes, and snippets.

@christianscott
Created May 26, 2019 03:33
Show Gist options
  • Save christianscott/bb44361954e79166ad16dca821e9e4f1 to your computer and use it in GitHub Desktop.
Save christianscott/bb44361954e79166ad16dca821e9e4f1 to your computer and use it in GitHub Desktop.
Chunked HTTP responses in Go
package main
import (
"fmt"
"net/http"
"time"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
flusher, ok := w.(http.Flusher)
if !ok {
panic("expected http.ResponseWriter to be a flusher")
}
w.Header().Set("X-Content-Type-Options", "nosniff")
fmt.Fprintf(w, "Hello")
flusher.Flush()
time.Sleep(time.Second * 1)
fmt.Fprintf(w, ", world!")
})
fmt.Println("Server listening on http://localhost:9000")
http.ListenAndServe(":9000", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment