Solve the "strconv.Atoi parsing XXX \r" problem

    When programming with go-libp2p , sometimes strconv.Atoi: parsing “70 \r”: invalid syntax is encountered, as shown in Figure (1):

Figure (1) strconv.Atoi reports an error

    When this problem occurs, the win10 platform will add \r\n as a newline character at the end of the parameter after you press Enter, and your code only deals with the situation of \n, without considering \r\ n this situation.
    The solution is as follows:
    //Old code

		sendData = strings.Replace(sendData, "\n", "", -1)

    //Change to, new code

		sendData = strings.Replace(sendData, "\n", "", -1)
		sendData = strings.Replace(sendData, "\r", "", -1)		

    Although go-libp2p is a cross-platform package, it mostly deals with Linux situations. If you want to migrate go-libp2p to windows, you need to modify the go-libp2p source code.
    This kind of error is caused by the difference of the newline character (\n) between windows (\r\n) and Linux . The code should consider the difference of the platform and make corresponding modifications.

    Note: When reading input parameters from the console, Windows platform will automatically add \r\n at the end of this parameter , and Linux platform will automatically add \n at the end of this parameter .

Guess you like

Origin blog.csdn.net/sanqima/article/details/111299561