Reading os.Stdin, Unbuffered

1,875 views
Skip to first unread message

dc0d

unread,
Nov 27, 2017, 9:59:52 AM11/27/17
to golang-nuts
Is there a way to read from `os.Stdin` in an unbuffered way? (Not waiting for a `\n` or anything).

Jan Mercl

unread,
Nov 27, 2017, 10:06:35 AM11/27/17
to dc0d, golang-nuts
On Mon, Nov 27, 2017 at 4:00 PM dc0d <kaveh.sh...@gmail.com> wrote:

> Is there a way to read from `os.Stdin` in an unbuffered way? (Not waiting for a `\n` or anything).

        n, err := os.Stdin.Read(buf)

does not wait for `\n`. Or do you actually mean setting a terminal in raw mode? Because os.Stdin does not have to be a terminal.



--

-j

dc0d

unread,
Nov 28, 2017, 12:12:44 AM11/28/17
to golang-nuts
For example I want the program to exit, if any key has been pressed on keyboard. I can not find any way to skip the wait for a necessary split character (like '\n').

Axel Wagner

unread,
Nov 28, 2017, 2:15:44 AM11/28/17
to dc0d, golang-nuts
That is not Go specific; the OS does the line-buffering. You could use e.g. termbox to disable that.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

an...@aubble.com

unread,
Nov 28, 2017, 1:18:08 PM11/28/17
to golang-nuts
Use https://godoc.org/golang.org/x/crypto/ssh/terminal#MakeRaw to set raw mode on the stdin terminal.

Then you can just read from os.Stdin into a single byte buffer. If the read completes, then a key was pressed.

Robert Solomon

unread,
Nov 28, 2017, 8:02:59 PM11/28/17
to golang-nuts
I trying to learn how to use pseudo-terminal-go.  It works fine under Ubuntu 16.04 amd64.  But not fine on win10 64 bit.

go get github.com/carmark/pseudo-terminal-go/terminal

#github.com/carmark/pseudo-terminal-go/terminal
github.com\carmark\pseudo-terminal-go\terminal\terminal.go:715:15: Undefined State
github.com\carmark\pseudo-terminal-go\terminal\terminal.go:719:2:: Undefined Restore
github.com\carmark\pseudo-terminal-go\terminal\terminal.go:724:18: Undefined MakeRaw

I tried it with the -u flag also and got the same result.  And it doesn't matter if I use \ or / on that command line.

I also use github's termbox-go on this win10 box, and that works fine.

What's up?

--rob solomon

Marcus Franke

unread,
Nov 29, 2017, 12:53:49 AM11/29/17
to Robert Solomon, golang-nuts

Hi,

Looking at the repository I see there are termios_darwin_amd64, termios_freebsd_amd64, and termios_linux_amd64 files.

That package has code for these three operating systems, but no windows support.

A terminal on a different OS isn't as standardized as you seem to assume.


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

Robert Solomon

unread,
Nov 30, 2017, 7:20:37 PM11/30/17
to golang-nuts
Thanks 

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/NvD-pOTASIk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages