Java – convert c-shared library callbacks to other languages
I am developing a library that will be written in go and compiled into a C shared library, so it can be written in Python, node JS, Java, ruby and other languages call it
I just realized that there would be problems with callback How to call back calling code that is at least one of the above languages? Is there a way to do this, or do I need to implement something specific for each language in go?
Update definition:
I have been able to build go as a library and execute code in other languages, such as Java and python
My question specifically concerns the case where go runs something asynchronously and needs a callback to the caller (i.e. Java, python)
Solution
The common language of the interface between different programming languages is C. you can use something like CGO to make your go code accessible through C, and then call it using C or "native" binding in Python, Java, etc
Usually developers will only go through so much trouble for large well maintained projects If you have a personal project, if performance is not a problem, I suggest communicating through JSON (for Web Language) or stdout / stderr pipeline (maybe more you want) You can print commands from ruby to stdout to let go code process requests and report on stdout Although it is sometimes difficult to transfer both input and output to another program CGI programs work in this way, generating interpreters in other languages and pipelining data to the interpreter
Update: if you want to pass an object, it is difficult to call back Java / Python / any language runtime from go; I think this will need to be made differently according to the binding API (Java, python) If you have binding, maybe you have solved this problem? It may be easier if you can't pass any parameters to the callback (that is, it's basically a timer callback) Minimize data that needs to be generated in go and read in other languages