2

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?

Libin Wen
  • 434
  • 1
  • 5
  • 17
  • 4
    [Read a line and discard it](http://golang-examples.tumblr.com/post/41863665491/read-a-line-from-stdin). –  Aug 15 '13 at 15:55

4 Answers4

6

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

zzzz
  • 87,403
  • 16
  • 175
  • 139
  • Yes, I know readline from stdin can work. But that needs the user input a `Enter` key. I want to know if we can enter any key without the `Enter` key to realize the `pause` command. – Libin Wen Aug 16 '13 at 02:15
  • @wenlibin02: Have you actually read my answer? It _is_ there ;-) – zzzz Aug 16 '13 at 07:44
  • I am sorry that I did not understand it at first, Though I still don't know it now. Here is another realization: http://stackoverflow.com/a/17278730/2507321, it works well! – Libin Wen Aug 16 '13 at 15:55
6

Read a line from the standard input, then discard it (source):

bio := bufio.NewReader(os.Stdin)
line, hasMoreInLine, err := bio.ReadLine()
1

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")
Baba
  • 94,024
  • 28
  • 166
  • 217
-1

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")
Community
  • 1
  • 1
Libin Wen
  • 434
  • 1
  • 5
  • 17