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)
        }
    }
}
相關文章
相關標籤/搜索