Skip to content

nu1lspaxe/go-ffi-rust

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go-Rust FFI Example

This project demonstrates how to call Rust functions from Go using FFI (Foreign Function Interface).

Prerequisites

  • Go 1.16 or later
  • Rust and Cargo
  • A C compiler (gcc or clang)

Dev

Initialize the Rust Project and Generate Header File

cargo new rust_ffi --lib
cd rust_ffi
cbindgen --config cbindgen.toml --output src/rust_ffi.h

Building and Running

  1. First, build the Rust library:
cd rust_ffi
cargo build --release
  1. Copy the DLL file rust_ffi.dll to where main.go is.

  2. Then, run the Go program:

go run main.go

Project Structure

  • rust_ffi/ - Rust library containing the FFI functions
  • main.go - Go program that calls the Rust functions

Available Functions

The Rust library exposes the following functions:

  • add(a: i32, b: i32) -> i32: Adds two integers
  • multiply(a: i32, b: i32) -> i32: Multiplies two integers
  • greet(name: *const u8, name_len: usize) -> *mut u8: Returns a greeting string
  • free_string(s: *mut u8): Frees memory allocated by Rust

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published