I don't think so.

When I close the file and don't explicitly flush:

package main

import (
"bufio"
"fmt"
"io"
"os"
)

func main() {
sourceFilename := "MozillaFirefox-66.0.5-741.4.x86_64.rpm"
sourcef, err := os.Open(sourceFilename)
if err != nil {
fmt.Println("Error while opening source file:", err)
return
}
defer sourcef.Close()

destinationFilename := "CopiedMozillaFirefox-66.0.5-741.4.x86_64.rpm"
os.Create(destinationFilename)
destf, err := os.OpenFile(destinationFilename, os.O_APPEND|os.O_WRONLY, 
os.ModeAppend)
if err != nil {
fmt.Println("Error while opening destination file:", err)
return
}
defer func() {
fmt.Println("Closing file.")
destf.Close()
}()

fReader := bufio.NewReader(sourcef)
fWriter := bufio.NewWriter(destf)

n, err := io.Copy(fWriter, fReader)
if err != nil {
fmt.Println("Error while copying:", err)
return
}

fmt.Println("Copied", n, "bytes.")
}


Output:

➜  throttle-and-resume-download go run copyTest.go                          
                                            
Copied 46061104 bytes.
Closing file.
➜  throttle-and-resume-download diff MozillaFirefox-66.0.5-741.4.x86_64.rpm 
CopiedMozillaFirefox-66.0.5-741.4.x86_64.rpm
Binary files MozillaFirefox-66.0.5-741.4.x86_64.rpm and 
CopiedMozillaFirefox-66.0.5-741.4.x86_64.rpm differ
➜  throttle-and-resume-download echo $?
1


When I explicitly flush:

package main

import (
"bufio"
"fmt"
"io"
"os"
)

func main() {
sourceFilename := "MozillaFirefox-66.0.5-741.4.x86_64.rpm"
sourcef, err := os.Open(sourceFilename)
if err != nil {
fmt.Println("Error while opening source file:", err)
return
}
defer sourcef.Close()

destinationFilename := "CopiedMozillaFirefox-66.0.5-741.4.x86_64.rpm"
os.Create(destinationFilename)
destf, err := os.OpenFile(destinationFilename, os.O_APPEND|os.O_WRONLY, 
os.ModeAppend)
if err != nil {
fmt.Println("Error while opening destination file:", err)
return
}
defer func() {
fmt.Println("Closing file.")
destf.Close()
}()

fReader := bufio.NewReader(sourcef)
fWriter := bufio.NewWriter(destf)

n, err := io.Copy(fWriter, fReader)
if err != nil {
fmt.Println("Error while copying:", err)
return
}

// Flush writer to ensure all contents are written to file
err = fWriter.Flush()
if err != nil {
fmt.Println("Error while flushing writer:", err)
return
} else {
fmt.Println("Flushed writer.")
}

fmt.Println("Copied", n, "bytes.")
}


Output:

➜  throttle-and-resume-download go run copyTest.go                          
                                            
Flushed writer.
Copied 46061104 bytes.
Closing file.
➜  throttle-and-resume-download diff MozillaFirefox-66.0.5-741.4.x86_64.rpm 
CopiedMozillaFirefox-66.0.5-741.4.x86_64.rpm
➜  throttle-and-resume-download echo $?                                    
                                             
0






On Tuesday, May 21, 2019 at 6:39:08 PM UTC+5:30, Robert Engels wrote:
>
> You do not need to flush if you call close. Close will flush. There is 
> some other bug in your code I believe. 
>
> > On May 21, 2019, at 3:01 AM, Jan Mercl <0xj...@gmail.com <javascript:>> 
> wrote: 
> > 
> >> On Tue, May 21, 2019 at 9:44 AM <pierr...@gmail.com <javascript:>> 
> wrote: 
> >> 
> >> Usually though, you do not need to buffer the input or output at all. 
> > 
> > If I/O is performed in chunks comparable in size to what 
> > bufio.{Reader,Writer} uses then ok. When I/O is done in small chunks, 
> > then using bufio should improve performance. 
> > 
> > The question left is what the "usual" scenario is. 
> > 
> > -- 
> > You received this message because you are subscribed to the Google 
> Groups "golang-nuts" group. 
> > To unsubscribe from this group and stop receiving emails from it, send 
> an email to golan...@googlegroups.com <javascript:>. 
> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/CAA40n-WsWkLtnP19DUwHNSrjDNBncbVepqNxVj%3DUF6MFV8ARLA%40mail.gmail.com.
>  
>
> > For more options, visit https://groups.google.com/d/optout. 
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/f4541ff6-f94c-4a99-a403-797e5f5b566b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to