Build script: Run a command when building a Go module


I’ve a Go module that uses a Rust library. This Rust library has C bindings, so that I can use cgo to talk to Rust, which is nice. Everything works like a charm except the installation. Why? Because the Rust code needs to be compiled with cargo/rustc. I may embed the dynamic library (.so, .dylib, .dll), but it adds some complexity and I would like to avoid this approach in my current case.

I could tell the user to run go get -d …, then run go generate …, to finally run go install, where go generate would run the command to compile the Rust part (basically cargo build --release). However, I want my Go module to be used as a dependency of any other Go module (it’s a library, not a binary), and in such a situation, it’s not possible to run go generate automatically. If my understanding is correct: go install runs go build, which doesn’t run go generate.

So my question is: How can I run a command like cargo build --release when my module is being installed by a user as a dependency (so like a regular go get)? Is it even possible?


1 Like

I don’t think there’s any way to make go get compile both C and Rust code and link them with cgo. My suggestion would be to use Mage or Make to script the build.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.