我正在学习 Golang 使用 go v1.18 得到一些奇怪的输出当我打印时firstName, lastName, birthday, created只得到结果因为created 我已经打印声明变量并再次打印整个东西它工作正常问题是var reader = bufio.NewReader(os.Stdin)
package main
import (
"bufio"
"fmt"
"os"
"strings"
"time"
)
var reader = bufio.NewReader(os.Stdin)
func main() {
firstName := getUserData("Enter First Name: ")
lastName := getUserData("Enter Last Name: ")
birthdate := getUserData("Enter Date of Birth: ")
created := time.Now()
fmt.Println(firstName, lastName, birthdate, created) //only prints created
fmt.Println(firstName) // prints firstName
fmt.Println(lastName) // prints lastName
fmt.Println(birthdate) // prints birthdate
fmt.Println(created) // prints created
}
func getUserData(promptText string) string {
fmt.Print(promptText)
userInput, _ := reader.ReadString('\n')
cleanedInput := strings.Replace(userInput, "\n", "", -1)
return cleanedInput
}
富国沪深
相关分类