Standard library / mime/multipart
multipart.Writer.WriteField()
Create a form field and write its string value in one call. Expected stdout: 'Ada\n'.
Formal Syntax & Structure
go syntaxgo
func (w *Writer) WriteField(fieldname, value string) errorParameters & Attributes
| Name | Type | Description |
|---|---|---|
| fieldname | string | Multipart form field name. |
| value | string | Text value to write or associate. |
Return Value
error
Nil on success, otherwise the operation error.
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")) check(w.Close()) r := multipart.NewReader(&b, "demo") part := must(r.NextPart()) fmt.Println(string(must(io.ReadAll(part))))}
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) }}