我的内存笔记本的第三阶段 – Golang

huangapple go评论96阅读模式
英文:

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 (
&quot;bufio&quot;
&quot;fmt&quot;
&quot;os&quot;
&quot;strings&quot;
)
func readFromStdin() string {
fmt.Print(&quot;Enter a command and data: &quot;)
reader := bufio.NewReader(os.Stdin)
line, _ := reader.ReadString(&#39;\n&#39;)
//line = line[:len(line)-2]
line = strings.TrimSpace(line)
return line
}
func main() {
var notes []string
var maximumNotes int
for {
if maximumNotes &lt;= 0 {
fmt.Print(&quot;Enter the maximum number of notes: &quot;)
_, _ = fmt.Scan(&amp;maximumNotes)
}
line := readFromStdin()
var joinedNote string
var note []string
splittedString := strings.Split(line, &quot; &quot;)
if splittedString[0] == &quot;create&quot; &amp;&amp; len(notes) &gt;= maximumNotes {
fmt.Println(&quot;[Error] Notepad is full&quot;)
}
if splittedString[0] == &quot;create&quot; &amp;&amp; len(splittedString) &lt;= 1 {
fmt.Println(&quot;[Error] Missing note argument&quot;)
}
if splittedString[0] == &quot;create&quot; &amp;&amp; len(splittedString) &gt; 1 &amp;&amp; len(notes) &lt; maximumNotes {
i := 1
for ; i &lt; len(splittedString); i++ {
note = append(note, splittedString[i])
}
joinedNote = strings.Join(note, &quot; &quot;)
notes = append(notes, joinedNote)
fmt.Println(&quot;[OK] The note was successfully created&quot;)
}
if splittedString[0] == &quot;list&quot; &amp;&amp; len(notes) &lt;= 0 {
fmt.Println(&quot;[Info] Notepad is empty&quot;)
}
if splittedString[0] == &quot;list&quot; {
for i, noteList := range notes {
//newNote := strings.TrimSpace(noteList)
fmt.Printf(&quot;[Info] %d: %s\n&quot;, i+1, noteList)
}
}
if splittedString[0] == &quot;clear&quot; {
notes = nil
fmt.Println(&quot;[OK] All notes were successfully deleted&quot;)
}
if splittedString[0] == &quot;exit&quot; {
fmt.Println(&quot;[Info] Bye!&quot;)
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

# &#39;\n&#39; will be added in the next reading function, if any
buf.ReadString(&#39;\r&#39;)
# you finish with the string that you want plus &#39;\r&#39; at the end
buf.ReadString(&#39;\n&#39;)

This is happening because EOL in Windows system is represented by &#39;\r\n&#39;

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(&#39;\r&#39;)

and I tried executing this on Windows and it solved your problem.

huangapple
  • 本文由 发表于 2022年9月26日 23:06:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/73856142.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定