23 lines
535 B
Go
23 lines
535 B
Go
|
|
package scriptx
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"io"
|
||
|
|
)
|
||
|
|
|
||
|
|
// Buffer will read the contents of the source in an internal buffer until an EOF is encountered.
|
||
|
|
// It will then write the entire contents of the buffer to the writer. Useful when reading and
|
||
|
|
// writing to the same file.
|
||
|
|
func Buffer() func(r io.Reader, w io.Writer) error {
|
||
|
|
return func(r io.Reader, w io.Writer) error {
|
||
|
|
var bfr bytes.Buffer
|
||
|
|
if _, err := io.Copy(&bfr, r); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
if _, err := io.Copy(w, &bfr); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
}
|