func TxtReadLine(filepath string, filename string) (txtArray []string) {
// 计数
lineNumHave := 0 // 有值
lineNumNull := 0 // 空值
// 打开txt文件
file, err := os.Open(filepath + filename)
if err != nil {
fmt.Println("无效的txt文件", err)
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
// 按行处理txt
for scanner.Scan() {
lineTxt := strings.TrimSpace(scanner.Text())
if len(lineTxt) == 0 {
lineTxt = "null"
lineNumNull++
} else {
lineNumHave++
}
txtArray = append(txtArray, lineTxt)
}
return
}
未经允许不得转载:微信 美文-微信文章库-我的知识库 » 读取文本内容,按行返回切片