如何在“扫描”语句中添加提示?

时间:2018-09-17 00:31:58

标签: go input scanf prompt

问题:如何在GoLang中的Scan语句的开头添加提示?

当前输出:

Enter Phrase:
Hello World!
You typed: Hello world!

所需的输出:

Enter Phrase: Hello world!
You typed: Hello world!

我的代码:

package main

import(
    "fmt"
)

func main() {
    var phrase string
    fmt.Println("Enter Phrase: ")
    fmt.Scan(&phrase)
    fmt.Println("You typed: ", phrase)
}

P.S。很抱歉发布这样的基本问题。 我花了几个小时进行研究,但我确实找不到答案。

1 个答案:

答案 0 :(得分:2)

我知道了。
如果其他人有此问题,这是一个解决方案:
在扫描语句之前使用 fmt.Print

示例:

package main

import(
    "fmt"
)

func main() {
    var phrase string
    fmt.Print("Enter Phrase: ")
    fmt.Scanln(&phrase)
    fmt.Println("You typed: ", phrase)
}