go实现按行读取文件(附案例)
-
按行读取文件并筛选打印数据
func ReadLineFile(fileName string) error { if common.IsEmpty(fileName) { return errors.New("Read Line File: fileName is nil") } file, err := os.Open(fileName) if err != nil { fmt.Println(err) return errors.New("Read Line File: Open file error " + err.Error()) } //scan line scanner := bufio.NewScanner(file) for scanner.Scan() { fmt.Println(scanner.Text()) //other options //lineText := scanner.Text() //if strings.Index(lineText, "url") >= 0 { // lineTextSplit := strings.Split(lineText, ", url(") // lineTextSplit02 := strings.Split(lineTextSplit[1], ") ") // fmt.Println(lineTextSplit02[0]) //} } return nil }
func main() { //按行读取css的信息,然后strings解析出url service.ReadLineFile("attachment/css/montserrat.css") }
网友评论