Standard library / mime/multipart

multipart.Reader.ReadForm()

Parse form values and files. File content beyond the memory budget spills to disk; a further 10 MiB allowance covers non-file data and overhead. This is not a total body size cap. Remove temporary files. Expected stdout: 'Ada 5\n'.

Formal Syntax & Structure

go syntaxgo
func (r *Reader) ReadForm(maxMemory int64) (*Form, error)

Parameters & Attributes

NameTypeDescription
maxMemoryint64In-memory file budget in bytes; non-file parts have a separate allowance. Not a total upload limit.

Return Value

(*Form, error)

Parsed form or error; remove temporary files after use.

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())  form := must(r.ReadForm(0))  defer form.RemoveAll()  fmt.Println(form.Value["name"][0], form.File["file"][0].Size)}
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