golang 安装逐行读取txt
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
)
var records []string
func ReadTxt() {
f, err := os.Open("mytest.txt")
if err != nil {
fmt.Printf("Error: %s\n", err)
return
}
defer f.Close()
li := bufio.NewReader(f)
for {
a, _, c := li.ReadLine()
if c == io.EOF {
break
}
if strings.Contains(string(a), "//") {
records = append(records, string(a))
}
// fmt.Println(string(a))
}
if len(records) < 0 {
return
}
for _, v := range records {
fmt.Println(v)
}
}
本文来自博客园,作者:ljymoonlight,转载请注明原文链接:https://www.cnblogs.com/ljymoonlight/p/15820236.html
posted on 2022-01-18 22:38 ljymoonlight 阅读(139) 评论(0) 编辑 收藏 举报