如何在Golang回调函数中传递变量?

鲍勃·范·路易特(Bob van Luijt):

我有以下代码:

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)
}

我要解决的问题是这个,我要在service 里面 使用loopThroughDirs()我该怎么做呢?

PS:loopThroughDirs内部函数filepath.Walk()在Go中称为回调吗?

abhink:

您也可以尝试返回一个WalkFunc函数:

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
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在 wordpress/woocommerce $tabs 回调函数中传递变量?

如何在替换函数中传递变量

如何在javascript的match函数中传递变量?

如何在函数调用中传递变量?

如何在函数或类中传递变量引用?

如何在linearSearch函数中传递变量i?

如何在不同的函数中传递变量

在不可见的reCaptcha回调中传递变量

如何在JavaScript中将变量传递给回调函数

如何在Python中传递变量?

如何在printf中传递变量

如何在curl中传递变量?

如何在php的回调函数中传递参数?

如何在回调函数中包含变量?

如何在es6 promise中传递变量然后回调到外部

不能通过回调传递变量,也不能对函数进行承诺

如何在由第三方库调用的回调函数中传递/使对象/变量可访问?

如何在R中的for循环或lapply函数中传递变量名?

如何在v-on中的v-on:click或@click中的函数上传递变量?

如何在forEach循环中传递变量函数

如何在不同 .js 文件中的函数之间读取、更新和传递变量?

我应该如何在这个类中的函数之间传递变量?

如何在类中的函数之间传递变量,同时将它们定义为对象?

使用菜单项时,如何在Google Apps脚本中的函数之间传递变量?

如何在 draw.text() 函数而不是字符串中传递变量?

如何在 tkinter scale 小部件中调用函数并传递变量?

Nativescript Angular 2 - 如何在 tap 函数 (xml) 中传递变量?

如何不使用forloop传递变量以请求回调?

您可以通过引用python中的回调方法来传递变量吗?