Standard library / mime/multipart

multipart.Writer.CreatePart()

Start a section with caller-provided MIME headers; previous part writers become unusable. Validate header values before passing untrusted input. Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func (w *Writer) CreatePart(header textproto.MIMEHeader) (io.Writer, error)

Parameters & Attributes

NameTypeDescription
headertextproto.MIMEHeaderHeader text or MIME header map, according to its type.

Return Value

(io.Writer, error)

Writer for new part, or error.

Working Example

Example Snippetgo
package main
import (  "bytes"  "fmt"  "io"  "mime/multipart"  "net/textproto"  "strings")
func main() {  var b bytes.Buffer  w := multipart.NewWriter(&b)  check(w.SetBoundary("demo"))  h := textproto.MIMEHeader{"Content-Type": {"text/plain"}}  f := must(w.CreatePart(h))  must(io.WriteString(f, "hello"))  check(w.Close())  fmt.Println(strings.Contains(b.String(), "Content-Type: text/plain"))}
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