【发布时间】:2018-05-28 19:59:45
【问题描述】:
我正在尝试制作一个代码,以从文件夹链接我的所有文件进行扫描,并根据他的内容和他的姓名使用正则表达式按他的大小制作“前 10 名”。文件。通过它的内容,我用 goroutines 制作频道,但我不明白为什么每次我的 goroutines 都被锁定。这是我的代码:
package main
import (
"flag"
"fmt"
"io/ioutil"
"regexp"
"runtime"
"sort"
"sync"
"time"
)
var rName = ".php"
var rContent = "php"
var maxSize, minSize int64
var files_ten []File
func main() {
start := time.Now()
channelOne := make(chan File)
channelTwo := make(chan File)
var wg sync.WaitGroup
var path string
flag.StringVar(&path, "path", "", "Path to folder")
flag.Parse()
fmt.Println("Path=", path)
for i := 0; i < runtime.NumCPU(); i++ {
go check(channelOne, channelTwo, &wg)
}
go top10(channelTwo, &wg)
wg.Wait()
getFolder(path, channelOne, &wg)
fmt.Println("top 10", files_ten)
t := time.Now()
current := t.Sub(start)
fmt.Println(current)
}
type File struct {
Size int64
Name string
Path string
}
func (this File) GetSize() int64 {
return this.Size
}
func getFolder(path string, channelOne chan File, wg *sync.WaitGroup) {
folder, err := ioutil.ReadDir(path)
if err != nil {
fmt.Println("Error:", err)
return
}
for _, data := range folder {
if data.IsDir() {
var newFolder string = path + data.Name() + "/"
getFolder(newFolder, channelOne, wg)
} else {
wg.Add(1)
channelOne <- File{Size: data.Size(), Name: data.Name(), Path: path}
}
}
}
func check(channelOne chan File, channelTwo chan File, wg *sync.WaitGroup) {
for {
file := <-channelOne
rName := regexp.MustCompile(rName)
maxSize = 10000
minSize = 0
if rName.MatchString(file.Name) {
if file.Size <= maxSize && file.Size >= minSize {
f, err := ioutil.ReadFile(file.Path + "/" + file.Name)
if err != nil {
fmt.Println("Error:", err)
return
}
rContent := regexp.MustCompile(rContent)
if rContent.MatchString(string(f)) {
channelTwo <- file
} else {
wg.Done()
}
} else {
wg.Done()
}
} else {
wg.Done()
}
}
}
func sortFilesFromBiggestToLowerSize(arrayFile []File) []File {
sort.Slice(arrayFile, func(i, j int) bool {
return arrayFile[i].Size > arrayFile[j].Size
})
return arrayFile
}
func top10(channelTwo chan File, wg *sync.WaitGroup) []File {
for {
f := <-channelTwo
if len(files_ten) == 10 {
if f.Size > files_ten[0].Size || f.Size >
files_ten[len(files_ten)-1].Size {
files_ten = files_ten[:len(files_ten)-1]
files_ten = append(files_ten, f)
return sortFilesFromBiggestToLowerSize(files_ten)
}
} else {
sortFilesFromBiggestToLowerSize(files_ten)
return append(files_ten, f)
}
wg.Done()
return files_ten
}
}
这是每次编译时的错误:
go run filebysize.go --path=C:/wamp64/www/symfony/init/cours1/
Path= C:/wamp64/www/symfony/init/cours1/
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan send]:
main.getFolder(0xc04210a3c0, 0x3d, 0xc04204c0c0, 0xc04204e210)
C:/Users/Sahra/Documents/go/display/filebysize.go:72 +0x28a
main.getFolder(0xc04210a200, 0x32, 0xc04204c0c0, 0xc04204e210)
C:/Users/Sahra/Documents/go/display/filebysize.go:69 +0x151
main.getFolder(0xc04200e6c0, 0x26, 0xc04204c0c0, 0xc04204e210)
C:/Users/Sahra/Documents/go/display/filebysize.go:69 +0x151
main.getFolder(0xc042051f57, 0x22, 0xc04204c0c0, 0xc04204e210)
C:/Users/Sahra/Documents/go/display/filebysize.go:69 +0x151
main.main()
C:/Users/Sahra/Documents/go/display/filebysize.go:37 +0x2e0
goroutine 19 [chan send]:
main.check(0xc04204c0c0, 0xc04204c120, 0xc04204e210)
C:/Users/Sahra/Documents/go/display/filebysize.go:95 +0x2ec
created by main.main
C:/Users/Sahra/Documents/go/display/filebysize.go:32 +0x26d
goroutine 20 [chan send]:
main.check(0xc04204c0c0, 0xc04204c120, 0xc04204e210)
C:/Users/Sahra/Documents/go/display/filebysize.go:95 +0x2ec
created by main.main
C:/Users/Sahra/Documents/go/display/filebysize.go:32 +0x26d
goroutine 21 [chan send]:
main.check(0xc04204c0c0, 0xc04204c120, 0xc04204e210)
C:/Users/Sahra/Documents/go/display/filebysize.go:95 +0x2ec
created by main.main
C:/Users/Sahra/Documents/go/display/filebysize.go:32 +0x26d
goroutine 22 [chan send]:
main.check(0xc04204c0c0, 0xc04204c120, 0xc04204e210)
C:/Users/Sahra/Documents/go/display/filebysize.go:95 +0x2ec
created by main.main
C:/Users/Sahra/Documents/go/display/filebysize.go:32 +0x26d
goroutine 23 [chan send]:
main.check(0xc04204c0c0, 0xc04204c120, 0xc04204e210)
C:/Users/Sahra/Documents/go/display/filebysize.go:95 +0x2ec
created by main.main
C:/Users/Sahra/Documents/go/display/filebysize.go:32 +0x26d
goroutine 24 [chan send]:
main.check(0xc04204c0c0, 0xc04204c120, 0xc04204e210)
C:/Users/Sahra/Documents/go/display/filebysize.go:95 +0x2ec
created by main.main
C:/Users/Sahra/Documents/go/display/filebysize.go:32 +0x26d
goroutine 25 [chan send]:
main.check(0xc04204c0c0, 0xc04204c120, 0xc04204e210)
C:/Users/Sahra/Documents/go/display/filebysize.go:95 +0x2ec
created by main.main
C:/Users/Sahra/Documents/go/display/filebysize.go:32 +0x26d
goroutine 26 [chan send]:
main.check(0xc04204c0c0, 0xc04204c120, 0xc04204e210)
C:/Users/Sahra/Documents/go/display/filebysize.go:95 +0x2ec
created by main.main
C:/Users/Sahra/Documents/go/display/filebysize.go:32 +0x26d
exit status 2
【问题讨论】:
-
这是很多代码要理解,但有些东西看起来不太对劲:
top10的返回值被忽略了,看起来 waitgroup 用于计算除执行 goroutines 之外的其他内容。 -
你是对的,问题是在函数top 10中,我不需要返回任何东西,包括在if else语句中,非常感谢!!!!!!!!!!!!!!! !!!!!!!!!
标签: go deadlock channel goroutine