How to emulate the IF (scanf (...) in C language in Golang. ==eof) to control the termination of the input

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

It is believed that many friends who go to Golang from C + + will often feel a headache for input control in go ... Especially if you want to emulate the following code in the C language in go you will encounter no small confusion:

Char Input[100];while (1) {    if (scanf ("%s", input) = = EOF) {        printf ("Input end \ n");        break;    }}

The above code will terminate the input by entering a CTRL + Z (EOF) in the console:




The int scanf () function in the C language returns an int type with a value of only three cases:

(1) If everything is OK, return the number of characters entered, that is, the value >0

(2) If an error occurs, return 0

(3) If you encounter the Terminator EOF, return-1

Note The third: encountering The Terminator returns-1, which ends the input. That is, if we enter EOF (EOF is actually-1), then the input terminates.


So let's go and try it with FMT. SCANF () (int, error) function:

var input stringfor {ret, err: = Scanf ("%s\r\n", &input)//fmt. SCANF () has two return values}


We have to pay attention to these two return values, and the first return value represents FMT. SCANF () The number of characters successfully entered, and the second is the error returned.

It's a matter of course that we think of using the returned error value to try to implement the C CTRL + Z input in the console to end the input:

Import (. "FMT" "IO") func main () {var input stringfor {_, Err: = Scanf ("%s\r\n", &input) if err = = Io. EOF {Break}}}

But the experimental results show that this does not work, how to input CTRL + Z can not terminate the input!


By the right, FMT. SCANF () also returns a return value of type int, comparing the return value of SCANF () in the C language just now, will this improved version of C be used as an ancestor-to indicate EOF? Try it:

Import (. "FMT" "Text/scanner") func main () {var input stringfor {ret, _: = Scanf ("%s\r\n", &input) if ret = = scanner. EOF {   //or with RET = = -1break}}}
After the experiment it was found that it was not possible to end the input with CTRL + Z in the console.


So it's strange that the Go program gets the console's Ctrl + Z and doesn't think of it as EOF??? So decided to test the console input CTRL + Z exactly what entered into the test code as follows:

Import (. "FMT") func main () {var input stringfor {_, _ = Scanf ("%s\r\n", &input) printf ("Input length:%d\n", Len (Input)) printf (" The characters entered are:%s\n ", input) Printf (" The characters entered are encoded as:%d\n ", Input[0])}}

The running results show:


The ASCII code is 26 characters:



Well, when we entered CTRL + Z in the console, the original input code was 26. So that's how we're going to improve our previous code:

Import (. "FMT" "strings") func main () {var input stringfor {_, _ = Scanf ("%s\r\n", &input) var eof rune = 26if strings. Contains (Input, String (EOF)) {Printf ("input end \ n") Break}}}
This will enable the successful use of CTRL + Z to end the input:





If reproduced please specify the source: http://blog.csdn.net/gophers/article/details/20656301




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.