Standard library / mime/multipart
multipart.Writer
Generate multipart bodies sequentially. SetBoundary precedes part creation; Close finalizes the message without closing its destination. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
type Writer struct {}Working Example
Example Snippetgo
package main
import ( "bytes" "fmt" "io" "mime/multipart" "strings")
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()) fmt.Println(strings.Contains(b.String(), "--demo--"))}
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) }}