On Tue, Dec 19, 2017 at 9:40 AM, <roup...@gmail.com> wrote: > > Thanks for your response, are you referring to something like this: > /* > #include <stdio.h> > #include <stdlib.h> > > void parse(char* err, int status) { > // do work > } > */ > > > > > func Ccomputation(address *C.char) { > > ptr := uintptr(unsafe.Pointer(C.parse)) > > SomeGoComputation(C.GoString(address), func(errString string, code int){ > //passing the go callback values to the c callback wrapper > e := C.CString(errString) > c := C.int(code) > // then free both e and c via unsafe pointer > ptr(e, c) > }) > } >
No, I mean write a C function like void callback(uintptr_t fn, char* err, int status) { pfn = (void (*)(char*, int))fn; pfn(err, status); } and write Go code like C.callback(callback, e, c) Ian > On Monday, December 18, 2017 at 10:03:47 PM UTC-5, rou...@gmail.com wrote: >> >> Hey guys, >> >> I want to write a c wrapper around a go code that has a callback >> >> func SomeGoComputation(address string, callback func(string,int)) { >> _, err = // do some computation with address, maybe go over the >> network >> if (err != nil) { >> callback(err.Error(), 0) >> } >> // I mean you get the gist >> callback("",1) >> } >> >> I've having trouble writing the callback signature which in c would be >> void(*c_callback)(*char, int) >> >> //export Ccomputation >> func Ccomputation(address *C.char, // how to write that callback) { >> SomeGoComputation(C.GoString(address), func(errString string, code >> int){ >> //passing the go callback values to the c callback wrapper >> e := C.CString(errString) >> c := C.int(code) >> c_callback(e, c) >> // then free both e and c via unsafe pointer >> }) >> } >> >> >> I look around and some (articles or not related answer from go posts) say >> to use an interface, so I did try `(C.char*, C.int)interface {}` but I did >> get an error missing , at line where the callback was declared in the >> wrapper. >> >> Any ideas, all advice are welcome thanks .. :) > > -- > 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.