I ran into a little problem for which I only have an ugly solution. I can not imagine that I am the first, but I did not find any clues about SO.
In the following (intentionally simplified) example, I would like to have a receiver on the walk function, which is my filepath.WalkFunc .
package main import ( "fmt" "os" "path/filepath" ) type myType bool func main() { var t myType = true // would have loved to do something as: // _ = filepath.Walk(".", t.walk) // that does not work, use a closure instead handler := func(path string, info os.FileInfo, err error) error {return t.walk(path, info, err)} _ = filepath.Walk(".", handler) } func (t myType) walk(path string, info os.FileInfo, err error) error { // do some heavy stuff on t and paths fmt.Println(t, path) return err }
Func main() triggers walk() and due to the receiver t - walk() , I cannot find another way than using this ugly closure handler as an argument before filepath.Walk() . I would hope for something more fileWalk(".", t.walk) , but this does not work. This gives a compilation error "t.walk method is not an expression, should be called"
I believe that the solution for my closure is correct in this regard, or there are better options that I do not know about.
PS. This is one of several cases where I have to use this closure construct to pass a handler function that has a receiver. So, this question is more related to passing handler functions, not to filepath behavior.
Thanks for your suggestions.
source share