【发布时间】:2019-12-30 10:48:12
【问题描述】:
我有一个 Go 程序来读取类似于以下代码的文本文件:
package main
import (
"bufio"
"log"
"os"
)
func main() {
file, err := os.Open("test.txt")
if err != nil {
log.Fatalf("failed opening file: %s", err)
}
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
var txtlines []string
for scanner.Scan() {
txtlines = append(txtlines, scanner.Text())
}
file.Close()
}
游乐场:https://play.golang.org/p/cnDOEFaT0lr
该代码适用于所有文本文件,但使用 UCS-2 little endian 编码的文件除外。如何将文件转换为 UFT8 格式进行读取?
【问题讨论】: