实例代码
package main
import (
"bufio"
"io"
"os"
"strings"
"fmt"
"unsafe"
)
func ReadLine(fileName string) error {
f, err := os.Open(fileName)
if err != nil {
return err
}
buf := bufio.NewReader(f)
fmt.Println(unsafe.Sizeof(buf))
for {
line, err := buf.ReadString('\n')
line = strings.TrimSpace(line)
fmt.Println(line)
if err != nil {
if err == io.EOF {
return nil
}
return err
}
}
return nil
}
func main() {
filename := "./simple.txt"
ReadLine(filename)
}
参考地址
- Source file src/bufio/bufio.go:https://golang.org/src/bufio/bufio.go?s=1305:1355#L36
- Golang 逐行读取文件:http://studygolang.com/articles/2417
- Golang学习 – bufio 包-具体实现:http://www.cnblogs.com/golove/p/3282667.html