當(dāng)我輸入命令時(shí),在按下回車按鈕之前給一個(gè)空格,它工作正常,但如果沒有空格則不起作用我已經(jīng)嘗試了幾種方法來(lái)解決這個(gè)問題,但一直無(wú)法解決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) } } }
接收用戶輸入的簡(jiǎn)單 CLI
繁星點(diǎn)點(diǎn)滴滴
2023-02-14 18:14:05