Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pause a program?

I want to realize the pause function like windows shell command pause. For example,

$ go run script.go 
Any key to continue...     //Then any key to continue

On windows, we can use the system command by golang function exec.Command(). But how about on Linux? I want to use the command read -n1 -p "Any key to continue" as a compromised one, but it doesn't work in the function exec.Command("read", "-n", "1"). I tested a lot but failed to know why.

Besides, do anyone know how to achieve the pause function just by golang language itself without using the external command?

like image 683
Libin Wen Avatar asked Jun 17 '26 15:06

Libin Wen


1 Answers

Just read from stdin. The user would have to press enter for you program to continue. Or set the terminal into "raw" mode. For the later case, there's eg. nsf's termbox

like image 146
zzzz Avatar answered Jun 20 '26 07:06

zzzz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!