dccmmtop / notebook

个人博客记录
0 stars 0 forks source link

Go逐行读取文件 #38

Open dccmmtop opened 2 years ago

dccmmtop commented 2 years ago
package main

import (
    "bufio"
    "fmt"
    "io"
    "os"
)

func main() {

    filename := "./1.txt"
    f, err := os.Open(filename)
    if err != nil {
        fmt.Printf("read %s fail, err: %v\n", filename, err)
    }
    defer f.Close()
    reader := bufio.NewReader(f)

    for {
        line, _, err := reader.ReadLine()
        if err == io.EOF {
            fmt.Println("done!")
            break
        }
        fmt.Println(line)
        // TODO
    }
}