GoPatina

Go inside a Rust project

By Peter Sjolin, .

Open examples/rust-interop as a Rust developer and you find an ordinary cargo workspace with two crates. quote is a command-line application written by hand in Rust: it reads a rules file and an order, and prints a receipt. rules is the pricing engine it depends on, with discounts, shipping and tax. That second crate was written in Go and compiled by GoPatina.

What the Rust side sees

quote uses rules the way it would use any crate:

let rules = rules::parse(rules_text)?;
let quote = rules::price(&order, &rules)?;

The crate's API, listed in its api.txt, is in Rust types: parse(text: &str) -> Result<Vec<Rule>, Error> and price(lines: &[Line], rules: &[Rule]) -> Result<Quote, Error>, with plain structs of String, i64 and Vec. Error prints the Go error's text and works with ?. There is no GoPatina runtime to install and no foreign function interface: the generated code is safe Rust, and cargo builds it like the rest of the workspace.

What CI checks

The rust-interop job runs on every pull request:

How it works

A Go package other than main now compiles to a Rust library crate (RFC 0014, implemented in #102). Its exported functions, types and constants become public Rust items. GoPatina's borrow checker decides how each argument crosses the boundary: &str and &[T] when the function only reads it, String and Vec<T> when it keeps it, and api.txt says why for each one it keeps. A Go error result becomes Result<T, Error>, and an exported sentinel error becomes a function that returns it, so e == rules::err_empty() works as Go's == does.

The limits

Building the example also found three defects, each fixed with its own golden test: struct field comments that rustfmt aligns differently (#103), and library panics, api.txt and error parameters (#104). The correctness page lists every defect found so far.

Drafted with AI assistance; every figure links to its evidence.