内存记事本Golang开发第三阶段:重复打印提示问题求助
Golang内存记事本提示重复打印问题
我正在开发Golang版内存记事本的第三阶段,遇到一个问题:Enter a command and data: 这个提示会重复打印两次而非一次,找不到原因。
预期流程:CLI打印Enter the maximum number of notes: ,获取并保存用户输入后,打印一次Enter a command and data: 。但实际该提示会在同一行打印两次。
相关代码如下:
package main import ( "bufio" "fmt" "os" "strings" ) func readFromStdin() string { fmt.Print("Enter a command and data: ") reader := bufio.NewReader(os.Stdin) line, _ := reader.ReadString('\n') //line = line[:len(line)-2] line = strings.TrimSpace(line) return line } func main() { var notes []string var maximumNotes int for { if maximumNotes <= 0 { fmt.Print("Enter the maximum number of notes: ") _, _ = fmt.Scan(&maximumNotes) } line := readFromStdin() var joinedNote string var note []string splittedString := strings.Split(line, " ") if splittedString[0] == "create" && len(notes) >= maximumNotes { fmt.Println("[Error] Notepad is full") } if splittedString[0] == "create" && len(splittedString) <= 1 { fmt.Println("[Error] Missing note argument") } if splittedString[0] == "create" && len(splittedString) > 1 && len(notes) < maximumNotes { i := 1 for ; i < len(splittedString); i++ { note = append(note, splittedString[i]) } joinedNote = strings.Join(note, " ") notes = append(notes, joinedNote) fmt.Println("[OK] The note was successfully created") } if splittedString[0] == "list" && len(notes) <= 0 { fmt.Println("[Info] Notepad is empty") } if splittedString[0] == "list" { for i, noteList := range notes { //newNote := strings.TrimSpace(noteList) fmt.Printf("[Info] %d: %s\n", i+1, noteList) } } if splittedString[0] == "clear" { notes = nil fmt.Println("[OK] All notes were successfully deleted") } if splittedString[0] == "exit" { fmt.Println("[Info] Bye!") os.Exit(0) } } }
问题原因
问题出在fmt.Scan(&maximumNotes)的行为上:fmt.Scan只会读取输入中的数字部分,当用户输入数字后按下回车键,这个换行符会残留在标准输入缓冲区中。后续调用readFromStdin时,bufio.NewReader(os.Stdin).ReadString('\n')会直接读取到这个残留的换行符,导致readFromStdin函数立即返回一个空字符串(经过TrimSpace处理后),主循环随即再次调用readFromStdin,于是提示被打印了第二次。
解决方案
方案一:清理fmt.Scan后的残留换行符
在读取完maximumNotes后,手动读取并丢弃缓冲区里的剩余字符,包括换行符:
if maximumNotes <= 0 { fmt.Print("Enter the maximum number of notes: ") _, _ = fmt.Scan(&maximumNotes) // 丢弃缓冲区中剩余的换行符 bufio.NewReader(os.Stdin).ReadString('\n') }
方案二:统一使用bufio处理所有输入
避免混合使用fmt.Scan和bufio,把读取最大笔记数的逻辑也改用bufio实现,再转换为整数:
package main import ( "bufio" "fmt" "os" "strconv" "strings" ) func readFromStdin(prompt string) string { fmt.Print(prompt) reader := bufio.NewReader(os.Stdin) line, _ := reader.ReadString('\n') line = strings.TrimSpace(line) return line } func readIntFromStdin(prompt string) int { line := readFromStdin(prompt) num, _ := strconv.Atoi(line) return num } func main() { var notes []string var maximumNotes int for { if maximumNotes <= 0 { maximumNotes = readIntFromStdin("Enter the maximum number of notes: ") } line := readFromStdin("Enter a command and data: ") // 后续逻辑保持不变 var joinedNote string var note []string splittedString := strings.Split(line, " ") if splittedString[0] == "create" && len(notes) >= maximumNotes { fmt.Println("[Error] Notepad is full") } if splittedString[0] == "create" && len(splittedString) <= 1 { fmt.Println("[Error] Missing note argument") } if splittedString[0] == "create" && len(splittedString) > 1 && len(notes) < maximumNotes { i := 1 for ; i < len(splittedString); i++ { note = append(note, splittedString[i]) } joinedNote = strings.Join(note, " ") notes = append(notes, joinedNote) fmt.Println("[OK] The note was successfully created") } if splittedString[0] == "list" && len(notes) <= 0 { fmt.Println("[Info] Notepad is empty") } if splittedString[0] == "list" { for i, noteList := range notes { fmt.Printf("[Info] %d: %s\n", i+1, noteList) } } if splittedString[0] == "clear" { notes = nil fmt.Println("[OK] All notes were successfully deleted") } if splittedString[0] == "exit" { fmt.Println("[Info] Bye!") os.Exit(0) } } }
内容的提问来源于stack exchange,提问作者Jotham Ntekim
相关产品推荐
相关产品推荐

