Created
December 24, 2022 02:00
-
-
Save sago35/578ff2b74a6b331c7be5e2db89bcb8fe to your computer and use it in GitHub Desktop.
net/http server (chunked)
This file contains 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 ( | |
"io" | |
"log" | |
"net/http" | |
) | |
func main() { | |
err := run() | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
func handlerChunked(w http.ResponseWriter, r *http.Request) { | |
w.Header().Set("Transfer-Encoding", "chunked") | |
flusher, ok := w.(http.Flusher) | |
if !ok { | |
log.Fatal("error") | |
} | |
io.WriteString(w, "Chunk1\n") | |
flusher.Flush() | |
io.WriteString(w, "Chunk2Chunk2\n") | |
flusher.Flush() | |
//w.Header().Set("Content-Length", "0") | |
} | |
func handler(w http.ResponseWriter, r *http.Request) { | |
io.WriteString(w, "Chunk1\n") | |
} | |
func run() error { | |
http.HandleFunc("/chunked", handlerChunked) | |
http.HandleFunc("/", handler) | |
return http.ListenAndServe(":8080", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
httputil.DumpResponse() した結果は以下。
14 とか 0 とかが余分に追加される。
14 は 16 進数なので 0x14 で 20 であることに注意。