Program should be able to get input from stdin
on terminal, as follows:
echo foobar | program
However, in the source below for Program, the stdin
read blocks if the pipe is omitted:
package main
import (
"fmt"
"os"
)
func main() {
b := make([]byte, 1024)
r := os.Stdin
n, e := r.Read(b)
if e != nil {
fmt.Printf("Err: %s
", e)
}
fmt.Printf("Res: %s (%d)
", b, n)
}
So how can Program detect whether something is being piped to it in this manner, and continue execution instead of blocking if not?
... and is it a good idea to do so?