Created
December 14, 2018 13:28
-
-
Save Hperigo/0ccdf0de7b9ca5e6c98ed10f727510eb to your computer and use it in GitHub Desktop.
This file contains hidden or 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 ( | |
"fmt" | |
"io/ioutil" | |
"os" | |
"sync" | |
_ "sync/atomic" | |
_ "time" | |
) | |
type Node struct { | |
Name string | |
Id int32 | |
Parent *Node | |
Children []*Node | |
info os.FileInfo | |
nodeChannel chan *Node | |
wg sync.WaitGroup | |
} | |
func visitDir(self *Node, depth int) { | |
files, err := ioutil.ReadDir(self.Name) | |
if err != nil { | |
fmt.Println("error reading dir at: ", self.Name, " -- ", depth) | |
panic(err) | |
} | |
go func() { | |
self.wg.Wait() | |
close(self.nodeChannel) | |
}() | |
self.wg.Add(1) | |
for _, f := range files { | |
n := &Node{} | |
n.info = f | |
n.Parent = self | |
n.Name = self.Name + "/" + f.Name() | |
n.nodeChannel = make(chan *Node) | |
self.nodeChannel <- n | |
if f.IsDir() { | |
fmt.Println("*****> ", n.Name) | |
go visitDir(n, depth+1) | |
} | |
} | |
self.wg.Done() | |
} | |
func main() { | |
fmt.Println("starting...") | |
parent := &Node{} | |
parent.nodeChannel = make(chan *Node) | |
parent.Name = "/Users/henrique/Desktop/A" | |
visitDir(parent, 0) | |
/// Debug | |
fmt.Print("\n\n\n\n ------ FILES ------\n\n\n\n") | |
var len int | |
VisitFileInfo(parent, func(n *Node) { | |
fmt.Println(len, " ", n.Name) | |
len++ | |
}) | |
fmt.Println(len) | |
} | |
//VisitFileInfo recursivly visits a file info and it's children | |
func VisitFileInfo(info *Node, visitor func(*Node)) { | |
visitor(info) | |
for _, f := range info.Children { | |
VisitFileInfo(f, visitor) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment