101 The accepted answer was correct in early versions of Go. 100 contains the more recent idiomatic way to achieve this.
软件包bufio
中有功能ReadLine.
请注意,如果该行不能放入读取缓冲区,则函数将返回不完整的行.如果您希望通过对函数的单个调用始终读取程序中的整行,则需要将ReadLine
函数封装到您自己的函数中,该函数在for循环中调用ReadLine
.
bufio.ReadString('\n')
并不完全等同于ReadLine
,因为ReadString
无法处理文件的最后一行不以换行符结尾的情况.