Is it easy to call Go from Julia? What about Rust? What are the biggest challenges

Is it easy to call Go from Julia? What about Rust? What are the biggest challenges? What about the other direction, i.e. them calling Julia?
I’m looking for two things. One is that I could do microservices in a modern language that gives me small binaries that execute fast. The other would be to use libraries in one language in the other, so the amount of libraries would be a factor.

Note that the original poster on Slack cannot see your response here on Discourse. Consider transcribing the appropriate answer back to Slack, or pinging the poster here on Discourse so they can follow this thread.
(Original message :slack:) (More Info)

With Rust, one way to do it is to export Rust functions as C functions with FFI and build a dynamic lib, and then use ccall in the Julia side to call it.
See GitHub - felipenoris/JuliaPackageWithRustDep.jl: Example of a Julia Package with Rust dependency.

1 Like

There’s also jlrs - Rust that states “The main goal behind jlrs is to provide a simple and safe interface to the Julia C API that lets you call code written in Julia from Rust and vice versa.”

And a page in the manual Embedding Julia · The Julia Language

I’d like to know how well this works in practice from someone who used this kind of thing for a real purpose.

I wrapped a function from a Go library recently; Go can generate shared libraries with the c-shared build option, and then you can ccall into them. I think you basically need to write a C-interface on the Go side if it doesn’t have one already. And the c-shared option doesn’t let you pass in and out structs, just basic types. But it seems to work well subject to those limitations.

This is the little C-interface I wrote: https://github.com/JuliaPackaging/Yggdrasil/blob/6742e470592ab1aaac30a5146916554adcea879d/L/licensecheck/bundled/main.go and https://github.com/ericphanson/LicenseCheck.jl/blob/d1bc60332cae406cbff86dfeb7023195dcadb2c3/src/LicenseCheck.jl#L33 is the Julia-side call. I didn’t wrap the whole library, just exposed the functionality I wanted.

(That was my first time writing Go/C code so if anyone sees an error please let me know! The unsafe stuff is a little frightening).

1 Like

Calling julia from go should be easy enough on the surface with Cgo and the julia c api. Be aware that Cgo is particular about pointers to pointers because they make it hard to manage memory. Memory management between two separate garbage collectors could be challenging if you try to share large data.

That looks like a perfect example of needing to interface to another language to gain functionality you really don’t want to write yourself! (Also it doesn’t look that easy to write.)

1 Like