On 10 September 2014 08:50, <
jgra...@gmail.com> wrote:
> Hi,
>
> thank you for your answer ehedgehog, but... I'm sorry, I can't get it. I
> don't know how to deal with MyStruct. How to pass it as param et get it from
> main.go ? I don't understand your function
> func (path string, info os.FileInfo, err error) error {
> return walker.WalkPath(&mystruct, info, err)
> }
>
> where/how should I use it as filepath.Walk
> expects the right signature...
func (path string, info os.FileInfo, err error) error {
return walker.WalkPath(&mystruct, info, err)
}
has the right signature. You can pass it to filepath.Walk in
main.main, which is where you'd declare mystruct.
When filepath.Walk calls the func, that will in turn call
WalkPath passing in the address of the structure.
>
> package walker
>
> import (
> "fmt"
> "os"
> )
>
> var counterDirs int
> var counterFiles int
> var sum int64
>
> type MyStruct struct {
> CounterDirs int
> CounterFiles int
> Sum int64
> }
>
> func WalkPath(path string, f os.FileInfo, err error) error {
>
func WalkPath(myValue *MyStruct, path string, f os.FileInfo, err error) error {
> myValue := &MyStruct{} // I know it shouldn't be here!!
remove
err := filepath.Walk(path, func (path string, info os.FileInfo, err
error) error {
return walker.WalkPath(&mystruct, info, err)
})
>
> if err != nil {
> log.Println(err.Error())
> }
>
> // access counterDirs and counterFiles and do something with them
> doSomethingWith(counterDirs, counterFiles, Sum)
> }
>
> Hard time with closures and pointers not fully understood...
Can you be more specific about the bits you don't
understand? Closures are just functions that access
variables declared outside the function, usually local
variables of an enclosing function; when the closure
runs it can tinker with those outside variables.
Chris
--
Chris "allusive" Dollin