翠花,上代码
package main import ( "bufio" "fmt" "os" "strings" ) func main() { //从标准输入读取数据 inputReader := bufio.NewReader(os.Stdin) fmt.Println("Please input your name:") //读取数据直到遇见\n位置 input, err := inputReader.ReadString('\n') if err != nil { fmt.Printf("An error occurred:%s\n", err) //异常错误后退出 os.Exit(1) } else { //用切片操作删除最后的\n name := input[:len(input)-1] fmt.Printf("Hello,%s!What can i di for you?\n", name) } for { input, err = inputReader.ReadString('\n') if err != nil { fmt.Printf("An error occurred:%s\n", err) continue } input = input[:len(input)-1] //全部转换为小写 input = strings.ToLower(input) switch input { case "": continue case "nothing", "bye": fmt.Println("Bye!") //正常退出 os.Exit(0) default: fmt.Println("Sorry,I didn't catch you.") } } }
有疑问加站长微信联系(非本文作者)