Skip to content

Instantly share code, notes, and snippets.

@mdeous
Created April 2, 2017 10:30
Show Gist options
  • Save mdeous/8afdfa87261ae7ad5ffc3ad54daf104f to your computer and use it in GitHub Desktop.
Save mdeous/8afdfa87261ae7ad5ffc3ad54daf104f to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io/ioutil"
"path"
)
func scanFile(targets *chan string, results *chan string) {
for target := range *targets {
fmt.Printf("file: %s\n", target)
*results <- target
}
close(*results)
}
func scanDir(dirname string, targets *chan string) {
fmt.Printf("dir: %s\n", dirname)
files, _ := ioutil.ReadDir(dirname)
for _, elem := range files {
elemPath := path.Join(dirname, elem.Name())
if elem.IsDir() {
scanDir(elemPath, targets)
} else {
*targets <- elemPath
}
}
}
func main() {
targets := make(chan string)
results := make(chan string)
go scanFile(&targets, &results)
scanDir("/home/mattoufoutu", &targets)
close(targets)
for result := range results {
fmt.Println(result)
}
}
$ ./golearn
dir: /home/mattoufoutu
dir: /home/mattoufoutu/.AndroidStudio
dir: /home/mattoufoutu/.AndroidStudio/config
dir: /home/mattoufoutu/.AndroidStudio/config/codestyles
dir: /home/mattoufoutu/.AndroidStudio/config/colors
dir: /home/mattoufoutu/.AndroidStudio/config/filetypes
dir: /home/mattoufoutu/.AndroidStudio/config/inspection
dir: /home/mattoufoutu/.AndroidStudio/config/keymaps
dir: /home/mattoufoutu/.AndroidStudio/config/options
file: /home/mattoufoutu/.AndroidStudio/config/options/abbreviations.xml
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan send]:
main.scanDir(0xc4200b6b10, 0x2f, 0xc42000c028)
/home/mattoufoutu/go/src/golearn/golearn.go:25 +0x2a4
main.scanDir(0xc4200b66c0, 0x27, 0xc42000c028)
/home/mattoufoutu/go/src/golearn/golearn.go:23 +0x229
main.scanDir(0xc4200b8aa0, 0x20, 0xc42000c028)
/home/mattoufoutu/go/src/golearn/golearn.go:23 +0x229
main.scanDir(0x4c3dbd, 0x11, 0xc42000c028)
/home/mattoufoutu/go/src/golearn/golearn.go:23 +0x229
main.main()
/home/mattoufoutu/go/src/golearn/golearn.go:34 +0x10a
goroutine 5 [chan send]:
main.scanFile(0xc42000c028, 0xc42000c030)
/home/mattoufoutu/go/src/golearn/golearn.go:12 +0x172
created by main.main
/home/mattoufoutu/go/src/golearn/golearn.go:33 +0xe7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment