DEV Community

Cover image for Harmonizing Go and Rust in a Single Application: A Weird Symbiotic Dance
Philip John Basile
Philip John Basile Subscriber

Posted on • Edited on

Harmonizing Go and Rust in a Single Application: A Weird Symbiotic Dance

It's no secret that the world of software development is both vast and diverse, brimming with a multitude of languages, each with their unique strengths. Among the more recent ones that have gained significant traction are Go and Rust. Go, known for its simplicity and speed, is loved by developers for creating scalable and efficient software. On the other hand, Rust, with its focus on memory safety without sacrificing performance, offers a great deal of control and predictability.

The question that naturally arises then is: Can these two powerful languages coexist within a single application? Can we leverage the strengths of both, while mitigating their weaknesses? The answer, quite excitingly, is yes. And this blog post is dedicated to explaining how it can be achieved.

The Intersection of Go and Rust

Go and Rust, though distinct, have overlapping areas of proficiency. Both are system level languages with low-level capabilities and are well suited for tasks such as building web servers, working with databases, or creating powerful APIs. But they each shine in different scenarios. Go is excellent for rapid development and straightforward concurrency, making it great for high-performance web servers. Rust, meanwhile, shines when it comes to handling complex logic requiring intense computational resources, or where strict memory safety is required.

But how does one harness the power of both within a single project? This is where Foreign Function Interfaces (FFI) come into play.

Foreign Function Interface (FFI)

A Foreign Function Interface (FFI) is a mechanism that allows programming languages to interoperate with code written in different languages. It provides a way for programs written in one language to call functions or use data structures defined in another language. Rust provides a well-documented FFI that allows it to interface with other languages like C, and, by extension, Go.

The primary purpose of an FFI is to enable seamless integration between different programming languages, allowing developers to leverage existing libraries, frameworks, or components written in a different language within their codebase.

An FFI provides a standardized interface that defines how to call functions or work with data across language boundaries. It enables communication and data exchange between different programming languages, such as C/C++, Python, Java, or Rust.

The FFI allows you to call functions written in a different language from your code. It typically involves providing function prototypes or bindings that describe the function signature, parameter types, and return values. The FFI mechanism handles the low-level details of marshaling data between languages.

In addition to function calls, an FFI allows you to exchange data between languages. This includes passing data structures, primitive types, arrays, or complex objects across language boundaries. The FFI handles the conversion of data representations to ensure compatibility between languages.

Depending on the FFI implementation, the level of abstraction provided can vary. Some FFIs offer a low-level interface that requires manual memory management and direct interaction with the foreign language's runtime. Others provide higher-level abstractions that automate memory management and provide a more idiomatic integration with the host language.

Different programming languages have their own FFI implementations or libraries. For example, in C/C++, you can use tools like "extern" declarations, header files, or libraries like "ctypes" in Python, "JNI" (Java Native Interface) in Java, or "CFFI" in Python for interacting with C code.

When using an FFI, it's important to consider performance implications. Crossing language boundaries can introduce overhead due to data marshaling, context switching, or other factors. Optimizations like minimizing the number of FFI calls or using techniques like just-in-time compilation can help mitigate performance issues.

FFIs are widely used to leverage existing codebases, access system libraries, or improve performance by writing performance-critical code in lower-level languages. They offer flexibility and extendibility by enabling software components from different languages to work together seamlessly.

Here's a general step-by-step guide to creating a Rust library and integrating it with a Go application.

Step 1: Writing the Rust Library

First, we'll create a Rust library that performs a memory-intensive operation.

#[no_mangle]
pub extern "C" fn process_data(input: *const c_char) -> *mut c_char {
    // Process the data
    // Remember to ensure memory safety!

    let output = /* result of computation */;
    CString::new(output).unwrap().into_raw()
}
Enter fullscreen mode Exit fullscreen mode

Here, the #[no_mangle] attribute is crucial as it tells Rust not to mangle the function name during compilation, allowing it to be called from our Go code.

Step 2: Building the Rust Library

We compile the Rust code into a shared library (.so file for Linux or .dll file for Windows).

$ rustc --crate-type cdylib -o output.so input.rs
Enter fullscreen mode Exit fullscreen mode

Step 3: Calling Rust from Go

With the shared library built, we can now call our Rust function from Go using the cgo tool.

/*
#cgo LDFLAGS: -L. -loutput
#include <stdlib.h>
extern char* process_data(char* input);
*/
import "C"
import "unsafe"

func ProcessData(input string) string {
    c_input := C.CString(input)
    defer C.free(unsafe.Pointer(c_input))

    c_output := C.process_data(c_input)
    defer C.free(unsafe.Pointer(c_output))

    return C.GoString(c_output)
}
Enter fullscreen mode Exit fullscreen mode

In the Go code, C.CString is used to convert the Go string to a C string (a null-terminated array of characters). After we're done with it, we use C.free to deallocate the memory, preventing a leak.

While this combination can bring substantial benefits to a project, there are also points to consider. There are performance implications when crossing the language boundary. Function calls via FFI can be slower due to additional overhead. Therefore, it's a good strategy to minimize the number of calls across this boundary. For example, if you have a loop in Go where each iteration requires a Rust function, it might be better to move the loop into Rust and make a single FFI call.

Furthermore, error handling between the two languages can get tricky. Rust's Result type doesn't translate directly to Go. Therefore, you'll need to implement a mechanism to translate Rust errors into a format that Go can understand and properly handle.

By enabling communication between Go and Rust, we can build applications that leverage Go's simplicity and rapid development while benefiting from Rust's memory safety and control over system resources. It's like having the best of both worlds.

This cooperation, however, comes with its own set of challenges, including function call overhead and error handling, which must be thoughtfully considered when structuring your application. But the rewards can be enormous, leading to applications that are faster, safer, and more efficient.

The fusion of Go and Rust in a single application showcases the power and versatility of modern software development – the ability to pick the right tool for each part of the job, and the means to make those parts work harmoniously together. It's a testament to the ever-evolving and exciting world of programming.

If you enjoy my technology-focused articles and insights and wish to support my work, feel free to visit my Ko-fi page at https://ko-fi.com/philipjohnbasile. Every coffee you buy me helps keep the tech wisdom flowing and allows me to continue sharing valuable content with our community. Your support is greatly appreciated!

Top comments (1)

Collapse
 
mlevkov_43 profile image
Maxim Levkov

Great topic. If you make a full application that depicts the points made in the article, it would serve a greater purpose and exposure. Thank you for the notes, though.