英文:
How to pause a program?
问题
我想实现类似于Windows命令行中的pause
功能。例如,
$ go run script.go
请按任意键继续... //然后按任意键继续
在Windows上,我们可以使用golang的exec.Command()
函数来执行系统命令。但是在Linux上怎么办呢?我想使用命令read -n1 -p "请按任意键继续"
作为一种妥协的方法,但在exec.Command("read", "-n", "1")
函数中它不起作用。我进行了很多测试,但是没有成功找出原因。
此外,有人知道如何仅通过golang语言本身实现暂停功能而不使用外部命令吗?
英文:
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?
答案1
得分: 6
只需从标准输入读取。用户需要按下回车键,你的程序才会继续执行。或者将终端设置为“原始”模式。对于后一种情况,可以使用 nsf 的 termbox。
英文:
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
答案2
得分: 6
从标准输入读取一行,然后丢弃它:
bio := bufio.NewReader(os.Stdin)
line, hasMoreInLine, err := bio.ReadLine()
英文:
Read a line from the standard input, then discard it (source):
bio := bufio.NewReader(os.Stdin)
line, hasMoreInLine, err := bio.ReadLine()
答案3
得分: 1
我不确定如果你不禁用输入缓冲的话是否能做到这一点,除非你取消输入缓冲,然后在终端上操作。这是与操作系统相关的,最好询问用户按下回车键。你还想捕获诸如Ctrl+Z
或Ctrl+C
等信号。
c := make(chan os.Signal)
d := make(chan bool)
// 捕获中断信号
signal.Notify(c, os.Interrupt, os.Kill)
// 捕获回车键
go func() {
bio := bufio.NewReader(os.Stdin)
bio.ReadByte()
d <- true
}()
fmt.Println("按任意键继续...")
// 阻塞
select {
case <-d:
case <-c:
}
fmt.Println("任务完成")
你可以参考这个简单的解决方案。
英文:
Am not sure if you can do that if you do not disable except you uncook then terminal (disable input buffering)> here is a simple solution to that
This is OS dependent to better ask the user the press enter. You also want to capture signals such as Ctrl+Z
or Ctrl+C
etc.
c := make(chan os.Signal)
d := make(chan bool)
// Capture Control
signal.Notify(c, os.Interrupt, os.Kill)
//Capture Enter
go func() {
bio := bufio.NewReader(os.Stdin)
bio.ReadByte()
d <- true
}()
fmt.Println("Any key to continue... ")
// Block
select {
case <- d :
case <- c :
}
fmt.Println("Mission Completed")
答案4
得分: -1
最后,我找到了以下方法:
在Linux Shell脚本中:
echo -ne "按任意键继续..."; stty -echo; read -n1; stty echo
在Linux的Golang中(参考:https://stackoverflow.com/a/17278730/2507321):
package main
import (
"fmt"
"os"
"os/exec"
)
func main() {
// 禁用输入缓冲
exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run()
// 不在屏幕上显示输入的字符
exec.Command("stty", "-F", "/dev/tty", "-echo").Run()
var b []byte = make([]byte, 1)
fmt.Printf("按任意键继续...")
os.Stdin.Read(b)
}
在Windows的Golang中:
exec.Command("cmd", "/C", "pause")
英文:
Finally, I found the following methods:
In Linux Shell Script:
echo -ne "press any key to continue..."; stty -echo; read -n1; stty echo
In Golang for Linux (ref: https://stackoverflow.com/a/17278730/2507321):
package main
import (
"fmt"
"os"
"os/exec"
)
func main() {
// disable input buffering
exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run()
// do not display entered characters on the screen
exec.Command("stty", "-F", "/dev/tty", "-echo").Run()
var b []byte = make([]byte, 1)
fmt.Printf("any key to continue...")
os.Stdin.Read(b)
}
In Golang for windows:
exec.Command("cmd", "/C", "pause")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论