I would like to read from the original stdin
of a Go program. For example, if I did echo test stdin | go run test.go
, I would want to have access to "test stdin". I've tried reading from os.Stdin
, but if there's nothing in it, then it will wait for input. I also tried checking the size first, but the os.Stdin.Stat().Size()
is 0 even when input is passed in.
What can I do?
Best Answer
Reading from stdin using
os.Stdin
should work as expected:Executing
echo test stdin | go run stdin.go
should print 'test stdin' just fine.It would help if you'd attach the code you used to identify the problem you encountered.
For line based reading you can use
bufio.Scanner
: