// Performs an add on every file under the directory supplied to the// method. Returns a map of the filename and its fixity falue and a// list of errors.func (p *Payload) AddAll(src string, hsh hash.Hash) (fxs map[string]string, errs []error) {fxs = make(map[string]string) // Mapping filepaths to fixity results.// Implementation of my Walkfuncvisit := func(pth string, info os.FileInfo, err error) error {hsh.Reset() // Reset the hash value everytime a new fixity starts.if err != nil {errs = append([]error{err})}if !info.IsDir() {dstPath := strings.TrimPrefix(pth, src)fx, err := p.Add(pth, dstPath, hsh) // run add on the file.if err != nil {return err}fxs[dstPath] = fx}return nil
}c := make(chan error)go func() {
c <- filepath.Walk(src, visit)}()if err := <-c; err != nil {errs = append([]error{err})}return}
I'm using filepath.Walk on a directory and processing each file I find there. I'd like the filepath.Walkfunc that I define to run as a goroutine but I'm fairly new to go so I'm having some trouble mapping the examples I've seen to an anonymous function like the one I defined for my walkfunc.Are there examples of using a walkfunc as a goroutine somewhere and is it advised to do it in this way with that function in a filepath.Walk?
I'm using filepath.Walk on a directory and processing each file I find there. I'd like the filepath.Walkfunc that I define to run as a goroutine but I'm fairly new to go so I'm having some trouble mapping the examples I've seen to an anonymous function like the one I defined for my walkfunc.Are there examples of using a walkfunc as a goroutine somewhere and is it advised to do it in this way with that function in a filepath.Walk?
package main
import (
"path/filepath"
"fmt"
"os"
)
func main(){
location := "../gocode/"
chann := GoWalk(location)
for msg := range chann {
fmt.Println(msg)
}
return
}
func GoWalk(location string) (chan string) {
chann := make(chan string)
go func(){
filepath.Walk(location, func(path string, _ os.FileInfo, _ error)(err error){
chann <- path
return
})
defer close(chann)
}()
return chann