How to simulate if (scanf(...)==EOF) in C language in Golang to control the termination of input

I believe that many friends who switch from C/C++ to Golang often feel headaches about input control in Go. . . Especially if you want to simulate the following code in the C language in Go, you will encounter a lot of confusion:

char input[100];
while (1) {
    
    
    if (scanf("%s", input) == EOF) {
    
    
        printf("输入结束\n");
        break;
    }
}

The above code can terminate the input by typing Ctrl+Z (EOF) in the console:
Insert picture description here

The int scanf() function in C language returns an int type, and its value has only three cases:
(1) If everything is normal, it returns the number of characters entered, that is, the value>0

(2) If an error occurs, return 0

(3) If the end character EOF is encountered, return -1

Note the third: it will return -1 when it encounters a terminator, which will end the input. In other words, if we enter EOF (EOF is actually -1), then the input will be terminated.

Then let's try it with Go, and use the fmt.Scanf() (int, error) function:

var input string
for {
    
    
	ret, err := Scanf("%s\r\n", &input) //fmt.Scanf()有两个返回值
}

We should pay attention to these two return values. The first return value represents the number of characters successfully entered by fmt.Scanf(), and the second is the error returned.

Of course, we would think of using the returned error value to try whether we can achieve C, and 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 is useless, no matter how input Ctrl+Z can not terminate the input!

By the way, fmt.Scanf() also returns a return value of type int. Compared with the return value of scanf() in the C language, will this improved version of the C language also use -1 to indicate EOF like its ancestors What? Try it:

import (
	. "fmt"
	"text/scanner"
)

func main() {
    
    
	var input string
	for {
    
    
		ret, _ := Scanf("%s\r\n", &input)
		if ret == scanner.EOF {
    
       //或者用ret == -1
			break
		}
	}
}

After experimenting, I found that this way, I can't use Ctrl+Z to end the input in the console.

This is weird. After getting the Ctrl+Z of the console, the Go program will not regard it as EOF? ? ? So I decided to test what was entered by typing Ctrl+Z in the console. The test code is as follows:

import (
	. "fmt"
)

func main() {
    
    
	var input string
	for {
    
    
		_, _ = Scanf("%s\r\n", &input)
		Printf("输入的长度为:%d\n", len(input))
		Printf("输入的字符为: %s\n", input)
		Printf("输入的字符的编码为:%d\n", input[0])
	}
}

The operation result shows: The Insert picture description here
characters with ASCII code 26 are: Insert picture description here
Well, when we input Ctrl+Z into the Go program in the console, the original code is 26. Then we improve our previous code like this:

import (
	. "fmt"
	"strings"
)

func main() {
    
    
	var input string
	for {
    
    
		_, _ = Scanf("%s\r\n", &input)
		var eof rune = 26
		if strings.Contains(input, string(eof)) {
    
    
			Printf("输入结束\n")
			break
		}
	}
}

In this way, you can successfully end the input with Ctrl+Z:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_52698632/article/details/113625296