例子:
file, err := os.Open("filename")
if err != nil {
//error handing
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
Scan() 默认对每行进行读取,可利用for语句进行循环遍历文件
Text() 将scanner读取的内容变为string

博客介绍了Golang中文件读取的方法。Scan()默认按行读取,可结合for循环遍历文件;Text()能将scanner读取的内容转换为字符串。

8万+

被折叠的 条评论
为什么被折叠?



