一个简单的命令行界面,接收用户输入。

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

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 (
	&quot;bufio&quot;
	&quot;fmt&quot;
	&quot;os&quot;
	&quot;strings&quot;
)
    func main() {
    	var notes []string
    	for {
    		fmt.Print(&quot;Enter a command and data: &quot;)
    		reader := bufio.NewReader(os.Stdin)
    		line, _ := reader.ReadString(&#39;\n&#39;)
    		var joinedNote string
    		var note []string
    
    		splittedString := strings.Split(line, &quot; &quot;)
    
    		if splittedString[0] == &quot;create&quot; &amp;&amp; len(splittedString) &gt; 1 {
    			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; || string(line) == &quot;list&quot; {
    			for i, noteList := range notes {
    				newNote := strings.TrimSpace(noteList)
    				fmt.Printf(&quot;[Info] %d: %s!\n&quot;, i, newNote)
    			}
    		}
    		if splittedString[0] == &quot;clear&quot; || line == &quot;clear&quot; {
    			notes = nil
    			fmt.Println(&quot;[OK] All notes were successfully deleted&quot;)
    		}
    
    		if splittedString[0] == &quot;exit&quot; || line == &quot;exit&quot; {
    			fmt.Println(&quot;[Info] Bye!&quot;)
    			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,&quot; &quot;) turned the input create \n into {&quot;create&quot;,&quot;\n&quot;}.

huangapple
  • 本文由 发表于 2022年9月20日 20:40:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/73786875.html
匿名

发表评论

匿名网友

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

确定