I have an implementation of the Unix tool cat
below. It reads a number of bytes from os.Stdin
into a buffer, then writes those bytes out to os.Stdout
. Is there a way I can skip the buffer and just pipe Stdin
directly to Stdout
?
package main
import "os"
import "io"
func main() {
buf := make([]byte, 1024)
var n int
var err error
for err != io.EOF {
n, err = os.Stdin.Read(buf)
if n > 0 {
os.Stdout.Write(buf[0:n])
}
}
}
You can use io.Copy()
(Documentation here)
Example:
package main
import (
"os"
"io"
"log"
)
func main() {
if _, err := io.Copy(os.Stdout, os.Stdin); err != nil {
log.Fatal(err)
}
}
For example,
package main
import (
"io"
"os"
)
func main() {
io.Copy(os.Stdout, os.Stdin)
}