Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Know if a go program is receiving stdin input or not
package main
import (
"io/ioutil"
"os"
"github.com/sirupsen/logrus"
)
func main() {
fi, err := os.Stdin.Stat()
if err != nil {
logrus.Error("error reading from stdin")
os.Exit(1)
panic(err)
}
logrus.Infof("File Mode %s", fi.Mode())
logrus.Infof("Os Mode Named Pipe %s", os.ModeNamedPipe)
logrus.Infof("Operation %s", fi.Mode()&os.ModeNamedPipe)
if fi.Mode()&os.ModeNamedPipe == 0 {
logrus.Infof("No STDIN Input")
} else {
var data []byte
data, err = ioutil.ReadAll(os.Stdin)
if err != nil {
logrus.Errorf("error reading from file %v", err)
}
logrus.Infof("There is STDIN '%s'", data)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment