简体   繁体   中英

How do I add a prompt to a Scan statement?

Question: How do I add a prompt to the beginning of a Scan statement in GoLang?

Current Output:

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

Desired Output:

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

My Code:

package main

import(
    "fmt"
)

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

PS I'm sorry for posting such an elementary question. I have spent several hours researching, and I genuinely cannot find the answer.

I figured it out.
Here is one solution in case other people have this problem:
use fmt.Print before your scan statement.

Example:

package main

import(
    "fmt"
)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM