Golang: How do I add a prompt to a Scan statement?

3889 views go
2

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)
}

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

answered question

1 Answer

5

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)
}

posted this

Have an answer?

JD

Please login first before posting an answer.