Standard library / mime/multipart
multipart.Part.Read()
Read bytes of the current part, stopping at the next boundary. Process n bytes even when an error is returned. Expected stdout: '3 Ada\n'.
Formal Syntax & Structure
go syntaxgo
func (p *Part) Read(d []byte) (n int, err error)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| d | []byte | Destination buffer for decoded part bytes. |
Return Value
(n int, err error)
Number of bytes read and error, including EOF.
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")) f := must(w.CreateFormFile("file", "demo.txt")) must(io.WriteString(f, "hello")) check(w.Close()) r := multipart.NewReader(&b, w.Boundary()) part := must(r.NextPart()) b2 := make([]byte, 3) n := must(io.ReadFull(part, b2)) fmt.Println(n, string(b2))}
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) }}