Standard library / mime/multipart

multipart.Reader

Sequential multipart parser. NextPart reads sections one at a time; ReadForm collects form data and may allocate temporary files. Expected stdout: 'name\nfile\n'.

Formal Syntax & Structure

go syntaxgo
type Reader struct {}

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())  for {    part, e := r.NextPart()    if e == io.EOF {      break    }    check(e)    fmt.Println(part.FormName())    check(part.Close())  }}
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