Golang 逐行读写之scanner.Scan

Go语言实现逐行读的方法多种,本文只介绍Scaner的方法,也是go推荐的方法。

官方文档

例子:

 file, err := os.Open("filename")
 if err != nil {

  //error handing

 }
 defer file.Close()
 scanner := bufio.NewScanner(file)
 for scanner.Scan() {
   fmt.Println(scanner.Text())
 }

 

画张简陋的图,帮助理解函数之间的调用关系

 

 

原文地址:https://www.cnblogs.com/DillGao/p/6234627.html