Created
May 26, 2019 03:33
-
-
Save christianscott/bb44361954e79166ad16dca821e9e4f1 to your computer and use it in GitHub Desktop.
Chunked HTTP responses in Go
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
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