📜  如何在 Golang 中逐行读取文件到字符串?

📅  最后修改于: 2021-10-25 02:08:44             🧑  作者: Mango

要逐行读取文件,请使用bufio 包 Scanner 。将文本文件命名为sample.txt,文件内容如下:

package main
  
import (
    "bufio"
    "fmt"
    "log"
    "os"
)
  
func main() {
  
    // os.Open() opens specific file in 
    // read-only mode and this return 
    // a pointer of type os.
    file, err := os.Open("sample.txt")
  
    if err != nil {
        log.Fatalf("failed to open")
  
    }
  
    // The bufio.NewScanner() function is called in which the
    // object os.File passed as its parameter and this returns a
    // object bufio.Scanner which is further used on the
    // bufio.Scanner.Split() method.
    scanner := bufio.NewScanner(file)
  
    // The bufio.ScanLines is used as an 
    // input to the method bufio.Scanner.Split()
    // and then the scanning forwards to each
    // new line using the bufio.Scanner.Scan()
    // method.
    scanner.Split(bufio.ScanLines)
    var text []string
  
    for scanner.Scan() {
        text = append(text, scanner.Text())
    }
  
    // The method os.File.Close() is called
    // on the os.File object to close the file
    file.Close()
  
    // and then a loop iterates through 
    // and prints each of the slice values.
    for _, each_ln := range text {
        fmt.Println(each_ln)
    }
}

输出:

在 Golang 中逐行读取文件到字符串