Standard library / mime/multipart
multipart.Writer.CreateFormFile()
Start a file part with a form field name and filename. Metadata does not prove file type or safety. Expected stdout: 'true\n'.
Formal Syntax & Structure
go syntaxgo
func (w *Writer) CreateFormFile(fieldname, filename string) (io.Writer, error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| fieldname | string | Multipart form field name. |
| filename | string | Client-supplied filename; treat as untrusted metadata. |
Return Value
(io.Writer, error)
Writer for new file part, or error.
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")) f := must(w.CreateFormFile("upload", "demo.txt")) must(io.WriteString(f, "hello")) check(w.Close()) fmt.Println(strings.Contains(b.String(), "filename=\"demo.txt\""))}
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) }}