Standard library / mime/multipart
multipart.File
Interface combining Reader, ReaderAt, Seeker and Closer; can represent memory or disk storage. Expected stdout: 'ello\n'.
Formal Syntax & Structure
go syntaxgo
type File interface { io.Reader io.ReaderAt io.Seeker io.Closer}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()) form := must(r.ReadForm(0)) defer form.RemoveAll() fh := form.File["file"][0] f2 := must(fh.Open()) defer f2.Close() var data multipart.File = f2 must(data.Seek(1, io.SeekStart)) fmt.Println(string(must(io.ReadAll(data))))}
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) }}