Standard library / net/http/httputil

httputil.NewChunkedWriter()

Write HTTP chunked-transfer framing. Close emits the terminating zero chunk but not the final CRLF following trailers. Do not use inside a normal HTTP handler, which already handles transfer framing. Expected stdout: '"3\\r\\nabc\\r\\n0\\r\\n"\n'.

Formal Syntax & Structure

go syntaxgo
func NewChunkedWriter(w io.Writer) io.WriteCloser

Parameters & Attributes

NameTypeDescription
wio.WriterUnderlying destination for chunked bytes.

Return Value

io.WriteCloser

Writer whose Close writes the terminal zero chunk, not the final trailer CRLF.

Working Example

Example Snippetgo
package main
import (  "bytes"  "fmt"  "net/http/httputil")
func main() {  var b bytes.Buffer  w := httputil.NewChunkedWriter(&b)  must(w.Write([]byte("abc")))  check(w.Close())  fmt.Printf("%q\n", b.String())}
func must[T any](v T, e error) T {  if e != nil {    panic(e)  }  return v}func check(e error) {  if e != nil {    panic(e)  }}
Go Reference