io.Reader
essentially reads the byte array ([]byte
).
type Reader interface { Read(p []byte) (n int, err error) }
bytes.Buffer
is used when you need a writer that writes to memory.
import ( "bytes" ) func main() []byte { // create buffer buf := new(bytes.Buffer) // run encoding on the `buf` err := jpeg.Encode(buf, new_image, nil) // convert buffer to reader reader := bytes.NewReader(buf.Bytes()) return buf.Bytes() }