In my Go program which I run on MS Windows I want to get a confirmation for a 'delete file' action with the keys y/n/a, and it's going to be repeated a big number of times. And I do not want to have to press enter for each time.
I've tried answers from Read a character from standard input in Go (without pressing Enter) without success.
- Answer based on
exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run()
It is for UNIX - Answer based on termbox-go , it is a bit overkill for what I need, just to read a char. I did not had the time to learn and use termbox-go
- getchar.go It is for OSX
- https://github.com/eiannone/keyboard It hangs and does not read the keystroke
- golang.org/x/term
term.MakeRaw(int(os.Stdin.Fd()))
gives the errorThe handle is invalid
- Next answer it is only for Linux
- The last answer does not show how to read a char
Any proposal for Windows? I find it a bit remarkable that Golang does not have a built-in method similar to getChar()
, or at least in the packages io
or bufio
, e.g., readRune or readByte still require Enter
, even when you're supposed to only read one of them.