go 语言简单的demo
package main import ( "fmt" "bufio" "os" "strings" ) func main() { // 从命令行获取输入 inputReader:=bufio.NewReader(os.Stdin) fmt.Println("please input your name:") input,err:=inputReader.ReadString('\n') if err!=nil{ fmt.Printf("an error occured:%s\n",err) // 出现异常退出 os.Exit(0) }else{ name:=input[:len(input)-1] fmt.Printf("Hello %s,what can i do for you?\n",name) } for{ input,err:=inputReader.ReadString('\n') if err!=nil{ fmt.Printf("an error occured:%s\n",err) // 出现异常退出 continue }else{ input:=input[:len(input)-1] input=strings.ToLower(input) switch input { case "": continue case "nothing","bye": fmt.Println("Bye!") os.Exit(0) default: if strings.Contains(input,"?"){ input=strings.Replace(input,"?","!",1) input=strings.Replace(input,"吗","",1) fmt.Println(input) }else{ fmt.Println("Sorry, I didn't catch you. Your question is too hard!!!") } } } } }