如何给Go程序添加暂停功能?

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

How to add pause to a Go program?

问题

当我执行一个Go控制台程序时,它只会在一秒钟内执行完毕,我已经在Google、Go官网和Stackoverflow上搜索过了。

  1. import (
  2. "fmt"
  3. )
  4. func main() {
  5. fmt.Println()
  6. }

当我执行它时,它立即关闭。

编辑2
实际上,我希望程序能够永久暂停,直到用户按下一个按钮。

英文:

When i ever execute a Go Console program it just executes in one second, I've been looking on Google, the Go website and Stackoverflow.

  1. import (
  2. "fmt"
  3. )
  4. func main() {
  5. fmt.Println()
  6. }

It closes immediately when i execute it.

EDIT 2
actually i wanted the program to permanently stay paused untill the user presses a button

答案1

得分: 87

你可以使用time.Sleep()来暂停程序任意长的时间。例如:

  1. package main
  2. import ("fmt"
  3. "time"
  4. )
  5. func main() {
  6. fmt.Println("Hello world!")
  7. duration := time.Second
  8. time.Sleep(duration)
  9. }

要任意增加暂停的时间,可以这样做:

  1. duration := time.Duration(10)*time.Second // 暂停10秒

编辑:由于问题的提问者增加了额外的限制,上面的答案不再适用。你可以通过创建一个新的缓冲读取器来暂停,直到按下<kbd>Enter</kbd>键为止,该读取器等待读取换行符(\n)字符。

  1. package main
  2. import ("fmt"
  3. "bufio"
  4. "os"
  5. )
  6. func main() {
  7. fmt.Println("Hello world!")
  8. fmt.Print("按下'Enter'键继续...")
  9. bufio.NewReader(os.Stdin).ReadBytes('\n')
  10. }
英文:

You can pause the program for an arbitrarily long time by using time.Sleep(). For example:

  1. package main
  2. import ( &quot;fmt&quot;
  3. &quot;time&quot;
  4. )
  5. func main() {
  6. fmt.Println(&quot;Hello world!&quot;)
  7. duration := time.Second
  8. time.Sleep(duration)
  9. }

To increase the duration arbitrarily you can do:

  1. duration := time.Duration(10)*time.Second // Pause for 10 seconds

EDIT: Since the OP added additional constraints to the question the answer above no longer fits the bill. You can pause until the <kbd>Enter</kbd> key is pressed by creating a new buffer reader which waits to read the newline (\n) character.

  1. package main
  2. import ( &quot;fmt&quot;
  3. &quot;bufio&quot;
  4. &quot;os&quot;
  5. )
  6. func main() {
  7. fmt.Println(&quot;Hello world!&quot;)
  8. fmt.Print(&quot;Press &#39;Enter&#39; to continue...&quot;)
  9. bufio.NewReader(os.Stdin).ReadBytes(&#39;\n&#39;)
  10. }

答案2

得分: 26

package main

import "fmt"

func main() {
fmt.Println("按下回车键以终止控制台屏幕!")
fmt.Scanln() // 等待回车键
}

英文:
  1. package main
  2. import &quot;fmt&quot;
  3. func main() {
  4. fmt.Println(&quot;Press the Enter Key to terminate the console screen!&quot;)
  5. fmt.Scanln() // wait for Enter Key
  6. }

答案3

得分: 15

最简单的另一种方法是使用最少的导入,使用以下两行代码:

  1. var input string
  2. fmt.Scanln(&input)

在程序的末尾添加这行代码,将暂停屏幕,直到用户按下回车键,例如:

  1. package main
  2. import "fmt"
  3. func main() {
  4. fmt.Println("按下回车键以终止控制台屏幕!")
  5. var input string
  6. fmt.Scanln(&input)
  7. }
英文:

The easiest another way with minimal imports use this 2 lines :

  1. var input string
  2. fmt.Scanln(&amp;input)

Adding this line at the end of the program, will pause the screen until user press the Enter Key, for example:

  1. package main
  2. import &quot;fmt&quot;
  3. func main() {
  4. fmt.Println(&quot;Press the Enter Key to terminate the console screen!&quot;)
  5. var input string
  6. fmt.Scanln(&amp;input)
  7. }

答案4

得分: 0

我只使用了一行代码fmt.Scanln

英文:
  1. import &quot;fmt&quot;
  2. func main() {
  3. fmt.Scanln()
  4. }

I use fmt.Scanln just one line.

huangapple
  • 本文由 发表于 2013年7月17日 11:51:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/17690776.html
匿名

发表评论

匿名网友

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

确定