程式語言 - Golang - Read File Line by Line



參考資訊:
https://stackoverflow.com/questions/8757389/reading-a-file-line-by-line-in-go

main.go

package main

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

func main() {
    file, _ := os.Open("test.txt")
    defer file.Close()

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

執行

$ echo 123 > test.txt
$ echo 456 >> test.txt
$ echo 789 >> test.txt

$ go run main.go
    123
    456
    789