似乎无法弄清楚为什么我收到错误消息:致命错误:所有 goroutine 都在睡觉 - 死锁!。
我怀疑下面的块中发生了竞争条件,该条件只能在通道关闭后执行。
我认为添加同步 WaitGroup 会有所帮助,但这只会给我带来这种僵局。我所看到的与我在网上看到的示例很接近,所以我不确定这里出了什么问题。
func S3UploadFolder(instance *confighelper.Instance, sess *session.Session,
srcFolder string, bucketName string) (err error) {
log.Println("S3UploadFolder", srcFolder, bucketName)
wg := &sync.WaitGroup{}
// find files recursively
walker := make(fileWalk)
wg.Add(1)
go func() {
// Gather the files to upload by walking the path recursively
if err := filepath.Walk(srcFolder, walker.Walk); err != nil {
log.Fatalln("Walk failed:", err)
}
wg.Done()
close(walker)
}()
wg.Wait()
for path := range walker {
// THE GO routine above needs to have finished by the time this for loop
// ranges over the channel
fmt.Println(path)
}
return
}
type fileWalk chan string
func (f fileWalk) Walk(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
f <- path
}
return nil
}
牛魔王的故事
潇潇雨雨
相关分类