问题描述
我无法在Go中找到
$ c> ReadLine code>不完全等于 ReadLine ,因为 ReadString 无法处理最后一行的文件不会以换行符结束。I'm unable to find file.ReadLine function in Go. I can figure out how to quickly write one, but just wondering if I'm overlooking something here. How does one read a file line by line?
There is function ReadLine in package bufio.
Please note that if the line does not fit into the read buffer, the function will return an incomplete line. If you want to always read a whole line in your program by a single call to a function, you will need to encapsulate the ReadLine function into your own function which calls ReadLine in a for-loop.
bufio.ReadString('\n') isn't fully equivalent to ReadLine because ReadString is unable to handle the case when the last line of a file does not end with the newline character.
这篇关于逐行阅读文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!