From cbaf13524360fbb3f799810c9f7d9d246abde76c Mon Sep 17 00:00:00 2001 From: Inanc Gumus Date: Wed, 6 Nov 2019 13:46:33 +0300 Subject: [PATCH] refactor: ifaces io reusable --- interfaces/14-io-reusable/main.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/interfaces/14-io-reusable/main.go b/interfaces/14-io-reusable/main.go index 391698c..fb66b85 100644 --- a/interfaces/14-io-reusable/main.go +++ b/interfaces/14-io-reusable/main.go @@ -9,17 +9,27 @@ package main import ( + "fmt" "io" "log" "os" ) func main() { - // stream from the standard input to the in-memory buffer in 32KB data chunks. - // os.Stdin.Read(...) -> os.Stdout.Write(...) - if _, err := io.Copy(os.Stdout, os.Stdin); err != nil { + n, err := transfer() + if err != nil { log.Fatal(err) } + fmt.Printf("%d bytes transferred.\n", n) +} + +func transfer() (n int64, err error) { + // stream from the standard input to the in-memory buffer in 32KB data chunks. + // os.Stdin.Read(...) -> os.Stdout.Write(...) + if n, err = io.Copy(os.Stdout, os.Stdin); err != nil { + return n, err + } + return n, err } // ioCopy streams from a file to another file.