Standard library / mime/multipart

multipart.NewReader()

Iterate over a multipart stream with the boundary from its Content-Type. Apply a separate total input limit for untrusted messages. Expected stdout: 'name\n'.

Formal Syntax & Structure

go syntaxgo
func NewReader(r io.Reader, boundary string) *Reader

Parameters & Attributes

NameTypeDescription
rio.ReaderUnderlying input stream; bound untrusted input before parsing.
boundarystringMultipart delimiter, without the leading two hyphens.

Return Value

*Reader

Reader wrapping the supplied input.

Working Example

Example Snippetgo
package main
import (  "bytes"  "fmt"  "io"  "mime/multipart")
func main() {  var b bytes.Buffer  w := multipart.NewWriter(&b)  check(w.SetBoundary("demo"))  check(w.WriteField("name", "Ada"))  f := must(w.CreateFormFile("file", "demo.txt"))  must(io.WriteString(f, "hello"))  check(w.Close())  r := multipart.NewReader(&b, w.Boundary())  part := must(r.NextPart())  fmt.Println(part.FormName())}
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