Golang processing input

Source: Internet
Author: User

Golang the processing of input is very convenient to me. This year's autumn strokes written, decisively abandon C + +.

Let's start with a few simple input processing.

1. Fmt. Scan

Fmt. The scan interactively accepts input via an empty glyd participle. When you call the scan function, you specify the variable name and number of variables to receive input.

The scan function does not return until all the specified variables have been received, and the carriage return cannot be returned in advance.

Fmt. Println ("Please enter the FirstName and Secondname:")

Fmt. Scan (&afirstname, &asecondname)

Fmt. Printf ("FirstName is%s, Secondname is%s\n", Afirstname, Asecondname)

The results are as follows:

Please enter the FirstName and Secondname:

Zz

Rr

FirstName is ZZ, Secondname is RR

2. Fmt. Scanln

When SCANLN is called, you also specify the variable name and number of variables to receive input.

It differs from scan in that \ n causes the function to return in advance and to empty the variable that has not received a value on return.

Fmt. Println ("Please enter the FirstName and Secondname:")

Fmt. Scanln (&bfirstname, &bsecondname)

Fmt. Printf ("FirstName is%s, Secondname is%s\n", Bfirstname, Bsecondname)

The results are as follows:

Please enter the FirstName and Secondname:

Zr

FirstName is zirconium, Secondname is

3. Fmt. Scanf

Processing input with scanf is a more flexible way of processing.

You need to specify the format of the input, suitable for fully understanding the input format of the scene, you can directly filter out the unwanted parts.

Fmt. Println ("Please enter the FirstName and Secondname:")

Fmt. SCANF ("//%s\n%s", &cfirstname, &csecondname)

Fmt. Printf ("FirstName is%s, Secondname is%s", Cfirstname, Csecondname)

The results are as follows:

1) In this scenario, when the input is received, the unwanted parts "//" and "\ n" are filtered out, and a useful two string ZZ and RR are received.

Please enter the FirstName and Secondname:

Zz

Rr

FirstName is ZZ, Secondname is RR

2) If the input does not conform to the specified format, start at the beginning of the non-conformance, and the variable value thereafter is empty.

Please enter the FirstName and Secondname:

ZR UI

FirstName is zirconium, Secondname is

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.