Base64 Encode File Streaming

Encode a file to base64 in chunks for memory-efficient binary data transport.

GoData Fetching

Code

snippet
// stream: encode reads in chunks to avoid loading the entire file into memory
package main
import ("encoding/base64";"io";"os")
func encodeBase64File(srcPath, dstPath string) error {
    src, err := os.Open(srcPath)
    if err != nil { return err }
    defer src.Close()
    dst, err := os.Create(dstPath)
    if err != nil { return err }
    defer dst.Close()
    encoder := base64.NewEncoder(base64.StdEncoding, dst)
    defer encoder.Close()
    _, err = io.Copy(encoder, src)
    return err
}