ljymoonlight

导航

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)
	}
}

posted on 2022-01-18 22:38  ljymoonlight  阅读(135)  评论(0编辑  收藏  举报