I need a command line utility to behave different if some string is piped into its STDIN. Here's some minimal example:
package main // file test.go
import (
"fmt"
"io/ioutil"
"os"
)
func main() {
bytes, _ := ioutil.ReadAll(os.Stdin)
if len(bytes) > 0 {
fmt.Println("Something on STDIN: " + string(bytes))
} else {
fmt.Println("Nothing on STDIN")
}
}
This works fine if you call it like that:
echo foo | go run test.go
If test.go
is called without anything on STDIN, the thing stucks at...
bytes, _ := ioutil.ReadAll(os.Stdin)
... waiting for EOF
.
What do I need to do to get this going?
Thanks in advance!
question from:
https://stackoverflow.com/questions/22744443/check-if-there-is-something-to-read-on-stdin-in-golang 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…