Created
September 3, 2016 13:07
-
-
Save nanoninja/311d688b156f027da6a67885fb5ecc6f to your computer and use it in GitHub Desktop.
Go HTTP Flusher Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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