I have the following code:
func loopThroughDirs(path string, fileInfo os.FileInfo, err error) error {
...do something with service...
return nil
}
func main() {
service, err := storage.New(client)
...
filepath.Walk(*dirName, loopThroughDirs)
}
The problem I want to solve is this, I want to use service
inside loopThroughDirs()
. How do I do this?
PS: Is the loopThroughDirs
function inside filepath.Walk()
called a callback in Go?
You can also try returning a WalkFunc
function :
func main() {
service, err := storage.New(client)
...
filepath.Walk(*dirName, getWalkFunc(service))
}
func getWalkFunc(service storage.Service) filepath.WalkFunc {
return func(path string, fileInfo os.FileInfo, err error) error {
// ...do something with service...
return nil
}
}
One way is by declaring loopThroughDirs
anonymously inside main
:
func main() {
service, err := storage.New(client)
...
filepath.Walk(*dirName, func(path string, fileInfo os.FileInfo, err error) error {
...do something with service...
return nil
})
}