douya6606 2019-09-05 02:11
浏览 101
已采纳

编写将匿名函数作为参数传递的高阶函数[关闭]

Here's how to reproduce this code: https://play.golang.org/p/ostuT1QFV4C**

I'm attempting to write a function that will allow me to pass any method that is used to get data and convert it to a string. This is an attempt to get a better understanding of how to use higher-order functions in Go.

func getConfigsFunc(getData func() ([]byte, error)) string {
    b, err := getData()
    if err != nil {
        fmt.Print(err)
    }
    str := string(b) // convert content to a 'string'
    return str
}

I've tried to use this function in a few different ways, but none of them are working right.

For example, in one of the cases in which I might use this, I could construct an anonymous function that closes around a variable and returns a function that accepts no parameters and returns ([]byte, error). Theoretically, this approach would allow me to wrap any code that gets data as long as I can return an anonymous method that accepts no parameters and returns ([]byte, error).

However, my attempts to implement this have failed.

First, I tried this:

getNfsConfigsFunc := func() ([]byte, error) {
        return ioutil.ReadFile("nfsConfigs.json")
    }()

However, I get:

assignment mismatch: 1 variable but func literal returns 2 values

(For reference, ioutil.ReadFile(..) returns ([]byte, error).)

I assumed (perhaps incorrectly) that my error meant that the anonymous function was executing immediately (like an IIFE in Javascript), so I tried wrapping the inner portion in a function (to try to force the anonymous function to return a function, rather than 2 values), like this:

getNfsConfigsFunc := func() ([]byte, error) {
        return nil, func() ([]byte, error) {
            return ioutil.ReadFile("nfsConfigs.json")
        }
    }()

but the IDE forces me to add "nil, " to part of the anonymous function, which seems to violate what I was intending to do (unless I'm misunderstanding something). Regardless, it gives me this error:

./main.go:247:20: assignment mismatch: 1 variable but func literal returns 2 values ./main.go:248:15: cannot use func literal (type func() ([]byte, error)) as type error in return argument: func() ([]byte, error) does not implement error (missing Error method)

Is it possible for me to do what I'm trying to do with Go?

For completeness, the contents of nfsConfigs.json look like this:

{
    "nfsURLBase" : "http://example.url.path.com/",
    "nfsFsBase" : "/data/nfs/path/to/files"
}
  • 写回答

1条回答 默认 最新

  • doutang2017 2019-09-05 03:09
    关注

    This thing

    getNfsConfigsFunc := func() ([]byte, error) {
            return ioutil.ReadFile("nfsConfigs.json")
    }()
    

    Does not create an anonymous function. It calls the anonymous function, because of the training().

    It should be just:

    getNfsConfigsFunc := func() ([]byte, error) {
            return ioutil.ReadFile("nfsConfigs.json")
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化