There is a program that reads two inputs from command line: username and password. The string "Password: " should be printed in console only after entering the username, but in my program it prints right away along with the first string "Username: ". How to fix this?
package main
import (
"bufio"
"os"
"fmt"
)
// A simple program that verifies the user by username and password.
var loginstatus bool
var username, password string
func login(username, password string) bool {
if username == "user123" && password == "pass123" {
return true
} else {
return false
}
}
func main() {
fmt.Print("Username: ")
user := bufio.NewScanner(os.Stdin)
fmt.Print("Password: ")
pass := bufio.NewScanner(os.Stdin)
for user.Scan() && pass.Scan() {
if login(user.Text(), pass.Text()) {
fmt.Println("Signed in.")
os.Exit(3)
} else {
fmt.Println("Incorrect username or password, please try again:")
}
}
}