英文:
A simple CLI that receives input from users
问题
当我输入命令时,在按下回车键之前加一个空格,它可以正常工作,但如果没有空格,它就无法正常工作。
我尝试了几种方法来解决这个问题,但都没有成功。
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
var notes []string
for {
fmt.Print("输入命令和数据:")
reader := bufio.NewReader(os.Stdin)
line, _ := reader.ReadString('\n')
var joinedNote string
var note []string
splittedString := strings.Split(line, " ")
if splittedString[0] == "create" && len(splittedString) > 1 {
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" || string(line) == "list" {
for i, noteList := range notes {
newNote := strings.TrimSpace(noteList)
fmt.Printf("[Info] %d: %s!\n", i, newNote)
}
}
if splittedString[0] == "clear" || line == "clear" {
notes = nil
fmt.Println("[OK] 所有笔记删除成功")
}
if splittedString[0] == "exit" || line == "exit" {
fmt.Println("[Info] 再见!")
os.Exit(0)
}
}
}
希望对你有帮助!
英文:
When I type in the command, give a space before hitting the enter button, it works fine, but it doesn't work if there is no space
I have tried several ways to fix this, but have been unable to
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
var notes []string
for {
fmt.Print("Enter a command and data: ")
reader := bufio.NewReader(os.Stdin)
line, _ := reader.ReadString('\n')
var joinedNote string
var note []string
splittedString := strings.Split(line, " ")
if splittedString[0] == "create" && len(splittedString) > 1 {
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" || string(line) == "list" {
for i, noteList := range notes {
newNote := strings.TrimSpace(noteList)
fmt.Printf("[Info] %d: %s!\n", i, newNote)
}
}
if splittedString[0] == "clear" || line == "clear" {
notes = nil
fmt.Println("[OK] All notes were successfully deleted")
}
if splittedString[0] == "exit" || line == "exit" {
fmt.Println("[Info] Bye!")
os.Exit(0)
}
}
}
答案1
得分: 1
这是因为你在捕获用户输入的行时包含了\n
,而且没有在它后面加上空格,导致\n
被附加到你要查找的单词上(create\n
不等于create
)。最简单的解决方法是手动删除末尾的\n
,可以使用line = line[:len(line)-1]
。
以下是更详细的解释。首先,ReadString
方法表示它包含了定界符,即\n
,你给定的输入是:
> ReadString会读取输入中第一个定界符(在本例中是\n
)之前的内容,并返回包含定界符在内的字符串。因此,我们知道line
的末尾始终会有\n
,除非你手动删除它。
当单词后面跟着一个空格时,你的代码可以正常工作,因为strings.Split(line," ")
将输入create \n
转换为{"create","\n"}
。
英文:
The reason for this is that you are including the \n
in line you capture from the user and without the space after it, the \n
gets tagged onto the word you are looking for ( create\n
does not equal create
). Easiest way to fix this is to manually remove the trailing \n
with line = line[:len(line)-1]
.
Here is a little more a deep dive. First the ReadString
method says it included the delimiter, in this case \n
, you give it:
> ReadString reads until the first occurrence of delim in the input, returning a string containing the data up to and including the delimiter. So we know line
will always have the \n
at the end of it unless you manually remove it.
Your code worked when the word was followed by a space because your strings.Split(line," ")
turned the input create \n
into {"create","\n"}
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论