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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测