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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵