Golang中的回调函数

huangapple go评论84阅读模式
英文:

Callback in Golang

问题

我正在使用go-couchbase将数据更新到couchbase,但是在如何使用回调函数方面遇到了问题。

函数Update要求我传递一个回调函数,该函数应为UpdateFunc类型:

func (b *Bucket) Update(k string, exp int, callback UpdateFunc) error

这是我所做的:

首先,我声明了一个类型UpdateFunc

type UpdateFunc func(current []byte) (updated []byte, err error)

然后在代码中,我添加了以下几行:

fn := UpdateFunc{func(0){}} 

然后调用Update函数:

bucket.Update("12345", 0, fn())

但是Go返回了以下错误:

语法错误:意外的字面量0,期望),对于这行代码fn := UpdateFunc{func(0){}}

那么我做错了什么?如何使回调函数工作?

附加信息

感谢你们的建议。现在我可以按照以下方式运行回调函数:

myfunc := func(current []byte)(updated []byte, err error) {return updated, err }

myb.Update("key123", 1, myfunc)

然而,当我运行bucket的Update函数时,我检查了couch数据库。具有键"key123"的文档消失了。似乎Update函数并没有更新值,而是删除了它。发生了什么?

英文:

I am using go-couchbase to update data to couchbase, however, I have problem in how to use the callback function.

The function Update requires me to pass a callback function in which should be UpdateFunc

func (b *Bucket) Update(k string, exp int, callback UpdateFunc) error

So that's what I have done

First, I declared a type UpdateFunc:

type UpdateFunc func(current []byte) (updated []byte, err error)

Then in the code, I add the following lines:

fn := UpdateFunc{func(0){}} 

and then call the Update function:

bucket.Update("12345", 0, fn()}

But the Go returns the following error:

syntax error: unexpected literal 0, expecting ) for this line fn := UpdateFunc{func(0){}}

So what I am doing wrong? So how can I make the callback function work ?

additional information

Thanks all of your suggestion. Now I can run the callback function as follows:

myfunc := func(current []byte)(updated []byte, err error) {return updated, err }

myb.Update("key123", 1, myfunc)

However, when I run the Update function of the bucket. I checked the couch database. The document with the key of "key123" was disappeared. It seems the Update does not update the value but delete it. What happened?

答案1

得分: 10

你需要创建一个与couchbase.UpdateFunc签名匹配的函数,然后将其传递给bucket.Update

例如:

fn := func(current []byte) (updated []byte, err error) {
    updated = make([]byte, len(current))
    copy(updated, current)
    // 修改 updated
    return
}

....

bucket.Update("12345", 0, fn)

请注意,要传递一个函数,只需传递fn而不是fn(),后者实际上会立即调用该函数并传递其返回值。

我强烈建议停下你正在做的一切,阅读Effective Go以及Go的博客上的所有文章,从First Class Functions in Go开始阅读。

英文:

You need to create a function that matches the couchbase.UpdateFunc signature then pass it to bucket.Update.

For example:

fn := func(current []byte) (updated []byte, err error) {
    updated = make([]byte, len(current))
    copy(updated, current)
    //modify updated
    return
}

....

bucket.Update("12345",0,fn)

Notice that to pass a function you just pass fn not fn(), that would actually call the function right away and pass the return value of it.

I highly recommend stopping everything you're doing and reading Effective Go and all the posts on Go's blog starting with First Class Functions in Go.

答案2

得分: 4

我在go-nuts列表上发布了相同的答案,以防万一:

你不需要自己定义UpdateFunc类型,它已经在这里定义了:

https://github.com/couchbaselabs/go-couchbase/blob/master/client.go#L608

只需正常定义函数并将其作为参数传递,或者像其他语言中那样传递一个匿名函数。

这里有一个简单的示例:

http://play.golang.org/p/YOKxRtQqmU

英文:

Same answer I posted on the go-nuts list, just in case:

You don't have to define the UpdateFunc type by yourself, it's already defined here:

https://github.com/couchbaselabs/go-couchbase/blob/master/client.go#L608

just define the function normally and pass it as an argument or pass an anonymous function as in other languages.

here's a simple example:

http://play.golang.org/p/YOKxRtQqmU

huangapple
  • 本文由 发表于 2014年8月14日 17:00:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/25304080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定