Skip to content

Instantly share code, notes, and snippets.

@nanoninja
Created September 3, 2016 13:07
Show Gist options
  • Save nanoninja/311d688b156f027da6a67885fb5ecc6f to your computer and use it in GitHub Desktop.
Save nanoninja/311d688b156f027da6a67885fb5ecc6f to your computer and use it in GitHub Desktop.
Go HTTP Flusher Example
// http.Flusher example
package main
import (
"fmt"
"net/http"
"time"
)
// curl -i http://localhost:3000
func home(rw http.ResponseWriter, r *http.Request) {
flusher, ok := rw.(http.Flusher)
if !ok {
panic("Expected http.ResponseWriter to be an http.Flusher")
}
for i := 1; i <= 5; i++ {
fmt.Fprintf(rw, "Chunk #%d\n", i)
flusher.Flush() // Trigger "chunked" encoding and send a chunk...
time.Sleep(500 * time.Millisecond)
}
}
func main() {
http.ListenAndServe(":3000", http.HandlerFunc(home))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment