Callback function in Golang

2,364 views
Skip to first unread message

Peter Hon

unread,
Aug 14, 2014, 4:59:37 AM8/14/14
to golan...@googlegroups.com
Hi.

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

  The fuction Update require 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 says 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 ?

Thanks

Jan Mercl

unread,
Aug 14, 2014, 5:34:25 AM8/14/14
to Peter Hon, golang-nuts
On Thu, Aug 14, 2014 at 10:59 AM, Peter Hon <peter...@gmail.com> wrote:
> fn := UpdateFunc{func(0){}}
>
>
> and then call the Update function:
>
>
> bucket.Update("12345",0,fn()}
>
>
>
> But the Go says the following error:
>
>
> syntax error: unexpected literal 0, expecting )
> for this line fn := UpdateFunc{func(0) { } }

I have no idea what is the above supposed to do, but it's definitely
invalid syntax. See http://golang.org/ref/spec#Function_literals

In short, inside the function literal parenthesis, there must be a
list of types ('func(int)' or 'func(string, bool)', ...) or list of
list of names folowed by a type ('func(a int, b, c strin)', ...).

'0' (a numeric literal) is not in those options.

-j

Francisco Dalla Rosa Soares

unread,
Aug 14, 2014, 5:43:24 AM8/14/14
to golan...@googlegroups.com
Let's start from the start:

1. Please, avoid the HTML emails, they really hurt in the eye.


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

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:



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Milan P. Stanic

unread,
Aug 14, 2014, 11:09:27 AM8/14/14
to golan...@googlegroups.com
On Thu, 2014-08-14 at 18:42, Francisco Dalla Rosa Soares wrote:
> Let's start from the start:
>
> 1. Please, avoid the HTML emails, they really hurt in the eye.

What about top-post? Does it hurt?

> 2. 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
>
> On Thu, Aug 14, 2014 at 5:59 PM, Peter Hon <peter...@gmail.com> wrote:
> > Hi.
> >
> > I am using go-couchbase to update data to couchbase, however, I have problem in how to use the callback function.
[...]

--
Kind regards, Milan

James Bardin

unread,
Aug 14, 2014, 12:12:34 PM8/14/14
to golan...@googlegroups.com

So we can keep our collective time-wasted to a minimum, Peter here seems to cross post everything from StackOverfow



Kevin Powick

unread,
Aug 14, 2014, 12:23:24 PM8/14/14
to golan...@googlegroups.com, m...@arvanta.net

On Thursday, 14 August 2014 16:09:27 UTC+1, mps wrote:
 
What about top-post? Does it hurt?

Not as much as quoting excessive amounts of irrelevant text from the post to which one is replying.

--
Kevin Powick

Milan P. Stanic

unread,
Aug 14, 2014, 12:44:06 PM8/14/14
to golan...@googlegroups.com
Dear Kevin,
If you allude to my previous post, I left it by intention to show the
context albeit I trimmed half of the message to which I replied.

N.B. sorry if my English is bad. I'm self taught in English.

--
Kind regards, Milan

Peter Hon

unread,
Aug 14, 2014, 10:27:18 PM8/14/14
to golan...@googlegroups.com
I am sorry that I cross post it in stackoverflow. I think there are different people reading in golang-nuts and stackoverflow. And the reponse are different. And I think they are two different sites. If cross post is the problem. I will tried to avoid it in future. Either I will post just one in golang-nuts or stackoverflow. By the way, I am not understand "avoid the HTML emails, they really hurt in the eye". Does it meant the email followed my name?

James Bardin於 2014年8月15日星期五UTC+8上午12時12分34秒寫道:

James Bardin

unread,
Aug 14, 2014, 10:38:14 PM8/14/14
to golan...@googlegroups.com
There's no problem posting in both forums to get more feedback, but as was commented on SO, it's polite to link between the two so people aren't duplicating effort. 

Peter Hon

unread,
Aug 15, 2014, 4:24:00 AM8/15/14
to golan...@googlegroups.com
Thanks James, you suggestion is nice. By the way, may I know the meaning of  "commented on SO", I am not knowing what is the SO stands for.

Francisco Dalla Rosa Soares

unread,
Aug 15, 2014, 4:36:02 AM8/15/14
to golan...@googlegroups.com
So = stack overflow

By HTML Mail hurting the eyes, I meant that you didn't need to use so much formatting in your email.  That it could be simple plain text like your later emails

Best regards 

2014年8月15日金曜日、Peter Hon<peter...@gmail.com>さんは書きました:
Thanks James, you suggestion is nice. By the way, may I know the meaning of  "commented on SO", I am not knowing what is the SO stands for.

--

Peter Hon

unread,
Aug 15, 2014, 5:02:39 AM8/15/14
to golan...@googlegroups.com
Thanks all of your suggestion. Now I can run the callback function as follows:

        //defining the function first and passing it as an argument
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?

Thanks
Reply all
Reply to author
Forward
0 new messages