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.")
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 the writer to write remaining bytes in the buffer.err = fWriter.Flush()if err != nil { fmt.Println("Error while flushing writer:", err)} else { fmt.Println("Flushed writer.")}fmt.Println("Copied", n, "bytes.")
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.")}
➜ 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.rpmBinary 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
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.")}
➜ 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
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> wrote:
>
>> On Tue, May 21, 2019 at 9:44 AM <pierr...@gmail.com> 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.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@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.
--
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...@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.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/D61080CC-6271-4782-88A8-9213A5C12A95%40ix.netcom.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAJhgacidQJLbjQ16BBDLOQ57q5BAbqJvu1Y57-itoAV9DtPDLQ%40mail.gmail.com.