逐行阅读文件

扫码查看
本文介绍了逐行阅读文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在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.

这篇关于逐行阅读文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 13:14
查看更多