refactor: ifaces io reusable
This commit is contained in:
@ -9,17 +9,27 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// stream from the standard input to the in-memory buffer in 32KB data chunks.
|
n, err := transfer()
|
||||||
// os.Stdin.Read(...) -> os.Stdout.Write(...)
|
if err != nil {
|
||||||
if _, err := io.Copy(os.Stdout, os.Stdin); err != nil {
|
|
||||||
log.Fatal(err)
|
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.
|
// ioCopy streams from a file to another file.
|
||||||
|
Reference in New Issue
Block a user