7

I am in the process of developing a library that will be written in Go and compiled down to a C shared library so it can be called by languages such as Python, Node.JS, Java and Ruby

I have just realised that there will be an issue when it comes to callbacks. How can I callback into the calling code which will be at least one of the above languages? Is there a single way I can do it or will I need to implement something specific on the Go side for each language?

Update for clarity:

I am already able to build Go as a library and execute the code from other languages such as Java and Python.

My question specifically relates to a situation where go is running something asynchronously and needs to call back into the caller (i.e. Java, Python).

jim
  • 8,670
  • 15
  • 78
  • 149

2 Answers2

4

The lingua franca of interfacing between different programming languages is to go through C. You can use something like cgo to make your Go code accessible through C, then use the C or "native" bindings in Python, Java, etc to invoke it.

Usually developers only go through this much trouble for large and well-maintained projects. If you have a personal project and if performance is less of a concern, I would suggest communicating via json (for web languages) or stdout/stderr pipes (probably more what you want). You can print commands to stdout from Ruby and have your Go code process the request and report back on its stdout. It's usually possible although sometimes tricky to pipe both input and output to another program. CGI programs work this way, spawning off interpreters for other languages and piping data to that interpreter.

Update: Calling back into the Java/Python/whatever language runtime from Go is difficult if you want to pass objects; I imagine that will need to be crafted differently depending on the binding API (Java, Python). Maybe you have already tackled this problem if you have the bindings in place? It might be easier if you can get away with not passing any parameters to the callback (i.e. it's basically a timer callback). Minimize the data which needs to be generated in Go and read in the other language.

Community
  • 1
  • 1
dwks
  • 602
  • 5
  • 10
  • Please post update. I can already compile the library and call from Java but I need a callback mechanism. – jim Apr 04 '16 at 07:02
  • Do you already have all the native APIs in place for these languages? If you do it'll probably be clear how a callback is invoked. – dwks Apr 04 '16 at 17:02
  • No, I don't have anything developed really other than PoC libs. that said I will look at the binding frameworks and see what they have in place. I plan on interfacing using JSON to minimise complexity. – jim Apr 04 '16 at 18:34
  • 1
    Native APIs are more performant but much more complicated than JSON. If you can communicate with JSON, your life will become much easier. Decide on a language-independent serialization format and you're good to go. – dwks Apr 05 '16 at 00:15
  • I agree. For this project, performance isn't an issue. I need to get up and running quick. I think we may go with a local socket connection and JSON interface. – jim Apr 05 '16 at 10:00
2

Well, this is gonna be a mostly link-only answer, however since Go 1.5 you can produce shared libraries that you can call from C, or any language that supports that (aka python, ruby, php, etc)

http://blog.ralch.com/tutorial/golang-sharing-libraries/

  • this is not my link, I've briefly followed it before to make a dummy python test.
OneOfOne
  • 95,033
  • 20
  • 184
  • 185
  • 2
    I guess the OP is asking regarding callbacks into other languages? For eg, if we have doSomethingJava(), which is written in Java, how to invoke it from Go? Making it accessible to Go code. Using c-shared libs, Java can access Go, but if there is a callback in Java code, how to invoke it from Go code? – a3.14_Infinity Apr 04 '16 at 04:58
  • @a3.14_Infinity Hmm, it'd probably work the same way you can do it to a C library but I'm not 100% sure how to. – OneOfOne Apr 04 '16 at 05:04
  • @OneOfOne it's tedious to research because everything links back to C code. I think I will reach out to some of the Go people at Google. – jim Apr 04 '16 at 07:03
  • Go code is able to call out to C, and Go code built with `-buildmode=c-shared` is able to do that either. If the OP talks about compiling their Go code as a C-shared library, then the target PL/runtime will see it as a C code anyway and hence *wll appear as a C program to that Go code either.* Hence I think calls should work both ways. – kostix Apr 04 '16 at 13:01