First, Introduction
Purpose: Use go language to write a simple chat robot, review the integration of the grammar and basic knowledge of the go language.
Software Environment: Go1.9,goland 2018.1.5.
Second, review
The basic elements of the Go language: identifiers, keywords, literals, separators, and operators. They can form various expressions and statements, and the latter do not have to end with semicolons.
- Identifier: The program entity, which is the name of the latter.
- Keyword: A sequence of characters reserved by a programming language that cannot be used as an identifier.
- Literal: A method of marking a value.
- operator = = Operator: the symbol used to perform a particular arithmetic or logical operation, called the operand.
Array: A sequence of several elements of the same type.
Slice (slice): Can be thought of as a wrapper form of an array of arrays that are called the underlying array of the slice.
Functions and methods: the declaration of a function usually includes the keyword func, the function name, the list of arguments wrapped by parentheses, and the result list, as well as the body of the function enclosed by curly braces.
int, divisor,int) (int, error) { }// function can have no argument list, or can have no result list, But the empty argument list must hold parentheses, and the empty result list does not have func printtab () {//}
Third, the procedure (first edition)
Code Warehouse Link: Https://github.com/OctopusLian/ChatRobot
Package Mainimport ("Bufio" "OS" "FMT" "Strings") Func main () {inputreader:= Bufio. Newreader (OS. Stdin)//ready to read data from standard inputFmt. Println ("Please input your name:") Input,err:= Inputreader.readstring ('\ n')//read the data until it hits \ n . ifErr! =nil{FMT. Printf ("An error occurred:%s") OS. Exit (1)//Exception Exit}Else { //delete the last \ n using the slice operationName: = Input[:len (Input)-1] FMT. Printf ("hello,%s! What can I does for you?\n", Name)} for{Input,err= Inputreader.readstring ('\ n') ifErr! =nil{FMT. Printf ("An error occurred:%s\n", Err)Continue} input= Input[:len (Input)-1] //convert all to lowercaseinput =strings. ToLower (Input)SwitchInput { Case "": Continue Case " Nothing","Bye": FMT. Println ("bye!") //Normal ExitOs. Exit (0) default: FMT. Println ("sorry,i didn ' t catch you.") } }}
Resources
Bufio
Os
Strings
Fmt
Using the go language to implement a simple chat robot