I doubt that the cgo_callback method has the type the C library
desires. from https://golang.org/ref/spec#Method_declarations:

          The type of a method is the type of a function with the
receiver as first argument.

I.e., the type of cgo_callback is func(a *A, ...).

The solution is to have a proper C function callback with the correct
type signature expected by the C side and resolve the correct (a *A)
to call inside the callback. Something like:

//export _callbackfunc
func _callbackfunc(fd C.int, context unsafe.Pointer) C._error_t {
      a := findA(context) // resolves the correct *A based on the context
      return C._error_t(a.Callback(int(fd)))
}

On Fri, Jun 17, 2016 at 3:23 AM,  <and...@breakoutcommerce.com> wrote:
> Hello,
>
> Could you guys please help me.
>
> I can't find how to export method function which belongs to some go-struct
> to C-code (cgo part) as callback. I know how to export simple function which
> not belongs to any type and use it as callback for C-function, but is it
> possibly to export method of concrete struct instance? since I need
> additional info when callback will be called from C-code. As example what
> I'm trying to explain:
>
> // extern int goCallbackHandler(int, int);
> //
> // static int doAdd(int a, int b) {
> //     return goCallbackHandler(a, b);
> // }
>
> import "C"
>
> type A struct {
> ... some data which used to process callback from C-side
> }
>
> // export cgo_callback
> func (a *A) cgo_callback(...){ <-----main problem here
> }
>
> main {
>    C.doAdd(...)
> }
>
> I can't modify c-side - I have only compiled library, so I don't know how to
> identify which instance of struct should be used to properly process C-call
> of go-side callback.
>
> Thanks,
> Andrey
>
> --
> 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+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to