Home > Article > Backend Development > How to optimize performance using HTTP file upload in Golang?
Best practices for optimizing Golang HTTP file upload performance: Set reasonable memory limits: r.MaxMemory = 32 << 20 // 32MB Use temporary files when storing large files: if err := r.ParseMultipartForm(32 << 20); err != nil { ... } Enable Goroutine concurrent processing: type multipartFile struct { ... }, func saveFilesConcurrently(files []multipartFile) { ... } Use compression algorithm to reduce file size :import "github.com/klauspost/compress/gzip", func compressImage(file multipartFile) { ... }Use CDN, cached response and code optimization
How to use HTTP file upload to optimize performance in Golang
Practice case: Optimizing image upload
Set reasonable memory limits
r.MaxMemory = 32 << 20 // 32MB
Use temporary files to store large files
if err := r.ParseMultipartForm(32 << 20); err != nil { return // 处理错误 } for _, file := range r.MultipartForm.File["images"] { f, err := os.CreateTemp("", "image-*.jpg") if err != nil { return // 处理错误 } if _, err := io.Copy(f, file); err != nil { return // 处理错误 } f.Close() // ... }
Enable Goroutine concurrent processing
type multipartFile struct { *multipart.FileHeader *os.File } func saveFilesConcurrently(files []multipartFile) { var wg sync.WaitGroup for _, file := range files { wg.Add(1) go func(f multipartFile) { defer wg.Done() // ... }(file) } wg.Wait() }
Use compression algorithm to reduce File Size
import "github.com/klauspost/compress/gzip" func compressImage(file multipartFile) (*os.File, error) { compressed, err := os.CreateTemp("", "image-*.jpg.gz") if err != nil { return nil, err } c := gzip.NewWriter(compressed) if _, err := io.Copy(c, file); err != nil { return nil, err } c.Close() return compressed, nil }
Additional Optimization Tips
Cache-Control
header to cache responses that typically do not change. The above is the detailed content of How to optimize performance using HTTP file upload in Golang?. For more information, please follow other related articles on the PHP Chinese website!