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