I am having a simple code that copies between two actual files. (They are on the same disk, but not sure if that is relevant.)
func copy(inPath, outPath string) {
inFile, err := os.Open(inPath)
if err != nil {
return fmt.Errorf("cannot open input file on path %q: %w", inPath, err)
}
defer inFile.Close()
outFile, err := os.Create(outPath)
if err != nil {
return fmt.Errorf("cannot create output file on path %q: %w", outPath, err)
}
defer outFile()
if _, err := io.Copy(inFile, outFile); err != nil {
return fmt.Errorf("cannot copy file %q to %q: %w", inPath, outPath, err)
}
}
I am now not sure, if I should or should not be using bufio
to either reader or writer or both or none.
That is, if I should do something like
func copy(inPath, outPath string) {
inFile, err := os.Open(inPath)
if err != nil {
return fmt.Errorf("cannot open input file on path %q: %w", inPath, err)
}
defer inFile.Close()
outFile, err := os.Create(outPath)
if err != nil {
return fmt.Errorf("cannot create output file on path %q: %w", outPath, err)
}
defer outFile()
if _, err := io.Copy(bufio.NewWriter(outFile), bufio.NewReader(inFile)); err != nil {
return fmt.Errorf("cannot copy file %q to %q: %w", inPath, outPath, err)
}
}
The bufio documentation does not tell me at all when to use it and when not.
CodePudding user response:
Do not use bufio in this scenario. When given an *os.File
destination, io.Copy copies the file by calling dest.ReadFrom(src)
. If the source is also an *os.File
, ReadFrom calls the OS to do the copy on some systems.
In the case where ReadFrom and other optimizations are not available, there's still no benefit to using bufio for io.Copy sources and destinations. The io.Copy buffer size is 32k and the bufio default buffer sizes are 4K. Because the bufio types bypass their own buffers when passed a larger buffer, the bufio types don't do anything other than allocate some extra memory.