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

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

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")
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题