Standard library / mime/multipart

multipart.Writer.CreateFormField()

Start a form field and return its body writer. Finish writing before creating the next part. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func (w *Writer) CreateFormField(fieldname string) (io.Writer, error)

Parameters & Attributes

NameTypeDescription
fieldnamestringMultipart form field name.

Return Value

(io.Writer, error)

Writer for new field, 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.CreateFormField("name"))  must(io.WriteString(f, "Ada"))  check(w.Close())  fmt.Println(strings.Contains(b.String(), "name=\"name\""))}
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