Standard library / mime/multipart
multipart.Form
Parsed form with Value and File maps keyed by field name. Call RemoveAll after closing opened files. Expected stdout: 'Ada 1\n'.
Formal Syntax & Structure
go syntaxgo
type Form struct { Value map[string][]string File map[string][]*FileHeader}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], len(form.File["file"]))}
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) }}