Hey Ian, Thanks again for replying, however I'm a bit confused by the code you've provided. am I passing the go callback into that function ?
I have few questions: 1- * I tried to do the do the following * /* #include <stdio.h> #include <stdlib.h> void parse(char* err, int status) { // will get called by the go function } */ import "C" func SomeGoComputation(address string) { _, err = // do some computation with address, maybe go over the network if (err != nil) { C.parse(C.CString(err.Error()), C.int(0)) //<-- call the function in the preamble } // I mean you get the gist C.parse(C.CString(""),C.int(1)) } //export C_compute func C_compute(C.char* address) { SomeGoComputation(C.GoString(address)) } *However, when I builded the go code to a shared object this I get this error * /* # command-line-arguments duplicate symbol _aBlock in: $WORK/command-line-arguments/_obj/_cgo_export.o $WORK/command-line-arguments/_obj/mygo.cgo2.o duplicate symbol _notification_parsing in: $WORK/command-line-arguments/_obj/_cgo_export.o $WORK/command-line-arguments/_obj/mygo.cgo2.o ld: 2 duplicate symbols for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) */ 2 - *Do all functions added into the preamble get exported to the header and the shared object that go build creates? * Thanks On Tuesday, December 19, 2017 at 10:35:49 PM UTC-5, Ian Lance Taylor wrote: > > On Tue, Dec 19, 2017 at 9:40 AM, <rou...@gmail.com <javascript:>> 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...@googlegroups.com <javascript:>. > > 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.