Files

63 lines
1.4 KiB
Go
Raw Permalink Normal View History

2019-11-04 11:01:04 +03:00
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
// For more tutorials : https://learngoprogramming.com
// In-person training : https://www.linkedin.com/in/inancgumus/
// Follow me on twitter: https://twitter.com/inancgumus
package main
import (
2019-11-06 13:46:33 +03:00
"fmt"
2019-11-04 11:01:04 +03:00
"io"
"os"
)
2019-11-04 12:09:15 +03:00
func main() {
2019-11-06 13:46:33 +03:00
n, err := transfer()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
2019-11-06 13:46:33 +03:00
}
fmt.Printf("%d bytes transferred.\n", n)
}
func transfer() (n int64, err error) {
2019-11-04 11:01:04 +03:00
// stream from the standard input to the in-memory buffer in 32KB data chunks.
2019-11-05 10:48:27 +03:00
// os.Stdin.Read(...) -> os.Stdout.Write(...)
2019-11-06 13:46:33 +03:00
if n, err = io.Copy(os.Stdout, os.Stdin); err != nil {
return n, err
2019-11-04 11:01:04 +03:00
}
2019-11-06 13:46:33 +03:00
return n, err
2019-11-05 10:48:27 +03:00
}
2019-11-04 11:01:04 +03:00
2019-11-05 10:48:27 +03:00
// ioCopy streams from a file to another file.
// we use it to stream from the standard input to ouput.
func ioCopy(dst, src *os.File) error {
// Use a fixed-length buffer to efficiently read from src stream in chunks.
buf := make([]byte, 32768)
// read over and over again to read all the data.
for {
// read can read only up to the buffer length.
nr, er := src.Read(buf)
// only write data if there is something to write.
if nr > 0 {
_, ew := dst.Write(buf[:nr])
if ew != nil {
return ew
}
}
// io.EOF = there is nothing left to read—close the loop.
if er == io.EOF {
return nil
}
// Only return an error if the reading really fails.
if er != nil {
return er
}
}
return nil
2019-11-04 11:01:04 +03:00
}