Excuse me if I am misunderstanding something - but it certainly looks to me 
like you are not at any point closing the bufio.Writer (because it is not a 
WriteCloser and does not support Close function). That is why you need to 
flush it! What you are closing is the underlying file/stream/WriteCloser, 
but it is the bufio.Writer that has the data in its buffer, *not* the file. 
This is not the same thing as flushing a file descriptor, which happens on 
close.

"After all data has been written, the client should call the Flush method 
to guarantee all data has been forwarded to the underlying io.Writer."

IF the bufio.Writer had been implemented as a WriteCloser, then closing it 
would probably flush as well - here is a discussion where they talk about 
how that could be implemented: 
https://stackoverflow.com/questions/43115699/how-to-get-a-bufio-writer-that-implements-io-writecloser

Howard

On Tuesday, May 21, 2019 at 8:42:51 AM UTC-5, Subramanian Sridharan wrote:
>
> 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.")
> }
>
>
>>

-- 
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/24d5242a-3e5a-4b83-a6f2-1ca7e29cfe0e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to