go - How to terminate early in Golang Walk? -


what's idiomatic way, if possible, of having go's filepath.walk return early?

i'm writing function find nested directory of given name. using filepath.walk can't see way of terminating tree-walking find first match.

func (*recursivefinder) find(needle string, haystack string) (result string, err error) {     filepath.walk(haystack, func(path string, fi os.fileinfo, errin error) (errout error) {         fmt.println(path)          if fi.name() == needle {             fmt.println("found " + path)             result = path             return nil         }          return     })      return } 

you should return error walkfunc. make sure no real error has been returned, can use known error, io.eof.

func find(needle string, haystack string) (result string, err error) {      err = filepath.walk(haystack,        filepath.walkfunc(func(path string, fi os.fileinfo, errin error) error {         fmt.println(path)          if fi.name() == needle {             fmt.println("found " + path)             result = path             return io.eof         }          return nil     }))      if err == io.eof {         err = nil     }      return } 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -