Last active
June 14, 2020 10:46
-
-
Save zhimoe/afa67365969361bd822cf7814b8a3e0c to your computer and use it in GitHub Desktop.
go_search_file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"io" | |
"io/ioutil" | |
"log" | |
"os" | |
"strings" | |
"sync" | |
"time" | |
) | |
var sem = make(chan bool, 700) //control the goroutine avoid open too many files | |
var wg sync.WaitGroup | |
func main() { | |
fmt.Println("start searching...") | |
start := time.Now() | |
dirname := "/home/test/txt/" | |
files, err := ioutil.ReadDir(dirname) | |
if err != nil { | |
log.Fatal(err) | |
} | |
wg.Add(len(files)) | |
fmt.Println(len(files)) | |
for _, f := range files { | |
sem <- true | |
go searchFile(dirname + f.Name()) | |
} | |
wg.Wait() | |
close(sem) | |
took := time.Since(start) | |
log.Printf("took %s", took) | |
} | |
func searchFile(path string) { | |
defer func() { <-sem }() | |
rw, _ := os.Open(path) | |
rb := bufio.NewReader(rw) | |
for { | |
line, _, err := rb.ReadLine() | |
if err == io.EOF { | |
break | |
} | |
var s = string(line) | |
if strings.Contains(s, "hello") { | |
fmt.Println(s) | |
} | |
} | |
wg.Done() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment