如何暂停一个程序?

huangapple go评论77阅读模式
英文:

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+ZCtrl+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 &lt;- true
}()


fmt.Println(&quot;Any key to continue... &quot;)

// Block
select {
	case &lt;- d :
	case &lt;- c :
}

fmt.Println(&quot;Mission Completed&quot;)

答案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 &quot;press any key to continue...&quot;; stty -echo; read -n1; stty echo

In Golang for Linux (ref: https://stackoverflow.com/a/17278730/2507321):

package main

import (
    &quot;fmt&quot;
    &quot;os&quot;
    &quot;os/exec&quot;
)

func main() {
    // disable input buffering
    exec.Command(&quot;stty&quot;, &quot;-F&quot;, &quot;/dev/tty&quot;, &quot;cbreak&quot;, &quot;min&quot;, &quot;1&quot;).Run()
    // do not display entered characters on the screen
    exec.Command(&quot;stty&quot;, &quot;-F&quot;, &quot;/dev/tty&quot;, &quot;-echo&quot;).Run()

    var b []byte = make([]byte, 1)

	fmt.Printf(&quot;any key to continue...&quot;)
	os.Stdin.Read(b)
}

In Golang for windows:

exec.Command(&quot;cmd&quot;, &quot;/C&quot;, &quot;pause&quot;)

huangapple
  • 本文由 发表于 2013年8月15日 23:51:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/18256504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定