How to simulate the IF (scanf (...) in C language in Golang ==eof) to control the terminated __c language of the input

Source: Internet
Author: User

It is believed that many friends who switch from C + + to Golang often have a headache for the input control in go ... Especially if you want to simulate the following code in the C language on go, you'll get a little confused:

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

The above code can terminate the input by entering a ctrl+z (EOF) in the console:




The int scanf () function in 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. In other words, if we enter EOF (EOF is actually-1), then the input is terminated.


Then we use go to try, with FMT. SCANF () (int, error) function:

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


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

We would certainly think of using the returned error value to try to achieve C so that you can enter ctrl+z in the console to end the input:

Import (
	. "FMT"
	"io"
)

func main () {
	var input string
	for {
		_, 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!


That's right, FMT. SCANF () also returns a return value of type int, comparing the return value of SCANF () in the C language, will this improved version of the C language also be used as an ancestor-one to show EOF. Give it a try:

Import (
	. "FMT"
	"Text/scanner"
)

func main () {
	var input string
	for {
		ret, _: = Scanf ("%s\r\n", &input)
		if ret = = scanner. EOF {   //or with RET = =-1 break
		}
}}
After the experiment, it was found that the input ctrl+z could not be used to end the input in the console.


So it's weird 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, the test code is as follows:

Import (
	. "FMT"
)

func main () {
	var input string
	for {
		_, _ = Scanf ("%s\r\n", &input)
		Printf ("The length of the input is: %d\n ", Len (Input)"
		printf ("Entered characters:%s\n", input)
		printf ("Input characters encoded as:%d\n", input[0])
	}
}

Run results show:


The ASCII code 26 characters are:



OK, so when we entered ctrl+z into the GO program in the console, the original entry code was 26. So we're going to improve our code in this way:

Import (
	. "FMT"
	"strings"
)

func main () {
	var input string for
	{
		_, _ = Scanf ("%s\r\n", &input)
		var eof rune =
		if strings. Contains (Input, String (EOF)) {
			Printf ("Enter end \ n") Break}}
This will be successful with ctrl+z to end the input:





If reproduced please indicate 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.