逐行阅读文件 [英] reading file line by line in go
问题描述
我无法在Go中找到 file.ReadLine
函数。我可以想出如何快速写出一个,但只是想知道我是否在这里忽略某些东西。如何逐行读取文件?
有函数ReadLine 包含在 bufio
中。
请注意如果该行不适合读取缓冲区,该函数将返回一个不完整的行。如果你想总是通过一次调用函数来读取程序中的整行,那么你需要将 ReadLine
函数封装到你自己的函数中,它调用<$ c
$ b
bufio.ReadString('\\\
')
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.
这篇关于逐行阅读文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!