How to write `cat` in Go using pipes

Viewed 2506

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])
        }
    }
}
2 Answers
Related