I just found a library that seems extremely useful and could save us a lot of work… but it’s written in Go. Can Rust interface with Go libraries? How would I go about doing that?
Can Rust interface with Go libraries?
C can interface with Go libraries, and hence Rust can too, via essentially the same process. I believe CGo will create a C header file, which can be converted into the appropriate Rust version via bindgen, or one can transliterate the Go signature to an appropriate Rust one manually. Something like:
//export GoFunction
func GoFunction(x int32) int64 { ... }
extern {
fn GoFunction(x: i32) -> i64;
}
(There is non-trivial overhead with calling into Go from a different language: on my machine a call from C to Go took ~900 ns, but from C to C or Rust took ~2 ns. However, this isn’t a reason not to do it, just something to keep in mind: there are many situations where the call overhead isn’t a concern at all.)
Linking to the Go library is best performed by creating a dynamic library that exports the functionality you need, and then using a Cargo build script to point cargo to the library (the build script could even invoke go ...
to build it).
Calling rust to c or c to rust
Cool. Does that catch panics on the boundary? Would be cool to measure the overhead of that too.
Go 1.5 can create libs that can be linked to a C program. This currently works for OSX and Linux, but not for Windows. Looks like this:
go build -buildmode=c-shared -o libhello.so hello.go.
So you have to search the Internet for Go and -buildmode=c-shared.
Good luck, Haddock