Undertook the study of Go.

Wrote a simple program:

package main import ( "fmt" ) func main() { fmt.Print("Enter a ft: ") var ft float64 fmt.Scanf("%f", &ft) output := ft * 0.3048 fmt.Println(output, "m") } 

After compiling it, after execution, it is instantly closed.

The question is how to avoid instant closing? (preferably a link to this topic in the documentation)

  • 2
    start cmd / terminal first, and start the program in it / it - etki
  • @Etki that is, the compiled exe will work only from the console? - Bastian
  • He will work from anywhere, he's exe's not ceasing to be. And the fact that you describe the feature of species behavior. - deterok
  • @Bastian, the compiled version will work anywhere, but after it runs, the terminal window / cmd opened specifically for it will disappear, as you have seen. - etki

2 answers 2

You can try this:

 package main import ( "fmt" ) func main(){ fmt.Scanf(" ") } 

But in general they do not do that, but simply execute the command from the terminal.

  • 2
    With an empty parameter Scanf() does not wait for input. If you add any character, then my machine starts to wait, for example, fmt.Scanf("\n") . You can use bufio , io/ioutil to read a line from standard input. +1 for "just execute the command from the terminal." - jfs
  • Corrected, my cant. The format string must be at least some. - deterok
  • And at the expense of bufio and others, it makes sense to import a bunch of mods even standard ones, fmt is already imported from it, so why not use Scanf. - deterok

Sample code, stopping the program after writing "q" in the terminal.

 fmt.Println("Press 'q' to quit") scanner := bufio.NewScanner(os.Stdin) for scanner.Scan() { exit := scanner.Text() if exit == "q" { break } else { fmt.Println("Press 'q' to quit") } } 
  • Answers should contain not only the code, but also an explanation. - AivanF.