golang从文件按行读取并输出

package main

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

func main() {
    file, err := os.Open("test.txt")
    defer file.Close()
    if nil == err {
        buff := bufio.NewReader(file)
        for {
            line, err := buff.ReadString('\n')
            if err == io.EOF {
                break
            }
            fmt.Print(line)
            time.Sleep(10*time.Millisecond)
        }
    }
}
posted @ 2018-12-25 17:16  sherlock-merlin  阅读(775)  评论(0编辑  收藏  举报