go的版本是1.4的
package main
import (
"bufio"
"flag"
"fmt"
"os"
"path/filepath"
"regexp"
)
var path *string = flag.String("p","./","搜索的路径")
var re_string *string = flag.String("r","","关键字")
func main() {
flag.Parse()
if *re_string == "" {
fmt.Println("搜索的关键字不能为空")
return
}
fmt.Println("搜索的路径:",*path,"搜索的关键字:",*re_string)
re,_ := regexp.Compile(*re_string)
filepath.Walk(*path,func(path string,info os.FileInfo,err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
fmt.Println("文件路径:",path)
File,_ := os.Open(path)
r := bufio.NewReader(File)
for {
b,_,e := r.ReadLine()
if e != nil {
break
}
if b,_ := regexp.Match(string([]byte{0}),b); b {
break
}
if re.Match(b) {
fmt.Println(string(b))
}
}
File.Close()
return nil
})
}
原文链接:https://www.f2er.com/go/190294.html