英文:
Not printing series of variable in golang
问题
我正在学习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("输入名字:")
lastName := getUserData("输入姓氏:")
birthdate := getUserData("输入出生日期:")
created := time.Now()
fmt.Println(firstName, lastName, birthdate, created) // 只打印了created
fmt.Println(firstName) // 打印firstName
fmt.Println(lastName) // 打印lastName
fmt.Println(birthdate) // 打印birthdate
fmt.Println(created) // 打印created
}
func getUserData(promptText string) string {
fmt.Print(promptText)
userInput, _ := reader.ReadString('\n')
cleanedInput := strings.Replace(userInput, "\n", "", -1)
return cleanedInput
}
希望对你有帮助!
英文:
I'm learning Golang got some weird output using go v1.18
when I print firstName, lastName, birthday, created
only got result as created
I have printed declaring variable and printing whole thing again it works fine
the problem is with 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
}
答案1
得分: 0
你需要使用strings.TrimSpace
而不是strings.Replace
,因为在你的代码中没有移除回车符\r
。
以下是修正后的代码:
package main
import (
"bufio"
"fmt"
"os"
"strings"
"time"
)
var reader = bufio.NewReader(os.Stdin)
func main() {
firstName := getUserData("输入名字:")
lastName := getUserData("输入姓氏:")
birthdate := getUserData("输入出生日期:")
created := time.Now()
fmt.Println(firstName, lastName, birthdate, created) // 打印全部信息
fmt.Println(firstName) // 打印名字
fmt.Println(lastName) // 打印姓氏
fmt.Println(birthdate) // 打印出生日期
fmt.Println(created) // 打印创建时间
}
func getUserData(promptText string) string {
fmt.Print(promptText)
userInput, _ := reader.ReadString('\n')
cleanedInput := strings.TrimSpace(userInput)
return cleanedInput
}
英文:
you have to use strings.TrimSpace
instead of strings.Replace
because in your code you did not remove the Carriage return \r
See the corrected code below.
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) //will print all
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.TrimSpace(userInput)
return cleanedInput
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论