How to read from standard input in the console?

后端 未结 11 1153
迷失自我
迷失自我 2020-11-28 00:54

I would like to read standard input from the command line, but my attempts have ended with the program exiting before I\'m prompted for input. I\'m looking for the equivalen

11条回答
  •  遥遥无期
    2020-11-28 01:30

    I think a more standard way to do this would be:

    package main
    
    import "fmt"
    
    func main() {
        fmt.Print("Enter text: ")
        var input string
        fmt.Scanln(&input)
        fmt.Print(input)
    }
    

    Take a look at the scan godoc: http://godoc.org/fmt#Scan

    Scan scans text read from standard input, storing successive space-separated values into successive arguments. Newlines count as space.

    Scanln is similar to Scan, but stops scanning at a newline and after the final item there must be a newline or EOF.

提交回复
热议问题