英文:
Third stage of my In-memory Notebook - Golang
问题
"输入一个命令和数据:" - 这个打印了两次而不是一次,我无法弄清楚为什么。
计划是"输入最大笔记数:"打印到CLI,获取并保存用户输入,然后打印"输入一个命令和数据:"。但它在同一行上打印两次。
以下是代码。
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func readFromStdin() string {
fmt.Print("输入一个命令和数据:")
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("输入最大笔记数:")
_, _ = fmt.Scan(&maximumNotes)
}
line := readFromStdin()
var joinedNote string
var note []string
splittedString := strings.Split(line, " ")
if splittedString[0] == "create" && len(notes) >= maximumNotes {
fmt.Println("[错误] 记事本已满")
}
if splittedString[0] == "create" && len(splittedString) <= 1 {
fmt.Println("[错误] 缺少笔记参数")
}
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] 笔记创建成功")
}
if splittedString[0] == "list" && len(notes) <= 0 {
fmt.Println("[信息] 记事本为空")
}
if splittedString[0] == "list" {
for i, noteList := range notes {
//newNote := strings.TrimSpace(noteList)
fmt.Printf("[信息] %d: %s\n", i+1, noteList)
}
}
if splittedString[0] == "clear" {
notes = nil
fmt.Println("[OK] 所有笔记已成功删除")
}
if splittedString[0] == "exit" {
fmt.Println("[信息] 再见!")
os.Exit(0)
}
}
}
英文:
"Enter a command and data: " - this prints twice instead of once and I'm unable to figure out why
The plan is "Enter the maximum number of notes: " prints to CLI, get and saves user input, then print "Enter a command and data: " prints. But it keeps printing twice on the same line.
Here is the code below.
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)
}
}
}
答案1
得分: 2
我假设你是在Windows系统上执行这个操作。
我对这个问题进行了研究,发现在Windows系统中使用bufio.ReadString存在问题。
# 下一个读取函数会添加'\n'
buf.ReadString('\r')
# 你得到的字符串末尾会有'\r'
buf.ReadString('\n')
这是因为Windows系统中的行尾表示为\r\n
。
更多信息请参考:https://groups.google.com/g/golang-nuts/c/hWoESdeZ398
所以在你的情况下,如果我们将这一行改为line, _ := reader.ReadString('\r')
,我尝试在Windows上执行,可以解决你的问题。
英文:
I assume that you are executing this on a Windows system.
I researched for this issue and found that there is an issue with
Windows system with bufio.ReadString
# '\n' will be added in the next reading function, if any
buf.ReadString('\r')
# you finish with the string that you want plus '\r' at the end
buf.ReadString('\n')
This is happening because EOL in Windows system is represented by '\r\n'
For more info on this https://groups.google.com/g/golang-nuts/c/hWoESdeZ398
So in your case if we change the line line, _ := reader.ReadString('\r')
and I tried executing this on Windows and it solved your problem.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论