golang学习笔记---逐行读取文件示例

package main

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

func printFile(filename string) {
    file, err := os.Open(filename)
    if err != nil {
        panic((err))
    }

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

func main() {

    printFile("hello.go")

}

 

posted on 2020-09-04 10:25  清明-心若淡定  阅读(734)  评论(0编辑  收藏  举报