Using the go language to implement a simple chat robot

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.