{"id":21659232,"url":"https://github.com/mediremi/rust-plus-golang","last_synced_at":"2026-01-08T06:16:22.013Z","repository":{"id":31759140,"uuid":"35325324","full_name":"mediremi/rust-plus-golang","owner":"mediremi","description":"Rust + Go — Call Rust code from Go using FFI","archived":false,"fork":false,"pushed_at":"2024-10-24T02:34:31.000Z","size":12,"stargazers_count":723,"open_issues_count":2,"forks_count":56,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-11-25T09:44:36.660Z","etag":null,"topics":["dynamic-library","ffi","go","golang","rust","static-library"],"latest_commit_sha":null,"homepage":"","language":"Makefile","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mediremi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-05-09T10:57:59.000Z","updated_at":"2024-11-25T02:21:23.000Z","dependencies_parsed_at":"2024-06-18T20:08:01.850Z","dependency_job_id":"91123b8b-1b0b-4b3a-800c-45cfaa1225d7","html_url":"https://github.com/mediremi/rust-plus-golang","commit_stats":null,"previous_names":["medimatrix/rust-plus-golang"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mediremi/rust-plus-golang","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mediremi%2Frust-plus-golang","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mediremi%2Frust-plus-golang/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mediremi%2Frust-plus-golang/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mediremi%2Frust-plus-golang/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mediremi","download_url":"https://codeload.github.com/mediremi/rust-plus-golang/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mediremi%2Frust-plus-golang/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265672333,"owners_count":23808842,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dynamic-library","ffi","go","golang","rust","static-library"],"created_at":"2024-11-25T09:30:42.304Z","updated_at":"2026-01-08T06:16:16.995Z","avatar_url":"https://github.com/mediremi.png","language":"Makefile","funding_links":[],"categories":["Makefile"],"sub_categories":[],"readme":"# Rust + Go(lang)\n\n\u003e TL;DR: Call Rust code from Go using FFI\n\n---\n\nThis repository shows how, by combining [`cgo`](https://blog.golang.org/c-go-cgo)\nand [Rust's FFI capabilities](https://doc.rust-lang.org/book/ffi.html), we can call\nRust code from Go.\n\nTwo ways of achieving this are presented in this repository: with a dynamic\nlibrary, and with a static library.\n\n## Running the example code\n\nRun `make run-all` to see `Rust + Go` in action, building and running two\nbinaries, one where the Rust code is compiled as a dynamic (sometimes also\nreferred to as a 'shared') library, and one where it is compiled as a static\nlibrary.\n\nYou should see the following output:\n\n```console\n$ make run-all\n   Compiling libc v0.2.132\n   Compiling hello v0.1.0 (/home/user/rust-plus-golang/lib/hello)\n    Finished release [optimized] target(s) in 0.1s\nHello world!\n(this is code from the dynamic library)\n    Finished release [optimized] target(s) in 0.00s\nHello world!\n(this is code from the static library)\n```\n\nYou will also find the binaries `./main_dynamic` and `./main_static` in your\ncurrent working directory.\n\n## How it works\n\nThe Rust code is packaged up into a dynamic library and a static library, and\nthe two Go binaries then call these using [`cgo`](https://blog.golang.org/c-go-cgo).\n\n## You can do this for your own project\n\n\u003e [Andrew Oppenlander's article on creating a Rust dynamic\n\u003e library](https://github.com/oppenlander/oppenlanderme/blob/master/public/articles/rust-ffi.md)\n\u003e is a great introduction to this topic.\n\n1. Begin by creating a `lib` directory, where you will keep your Rust libraries.\n2. Then, create a C header file for your library. [See the example `hello.h`](lib/hello.h).\n3. All that is left to do is to add some `cgo`-specific comments to your Go\n   code. These comments tell `cgo` where to find the library and its headers.\n\n### Dynamic library setup\n\nThe following `cgo` comments are required to use a dynamic library:\n\n```go\n/*\n#cgo LDFLAGS: -L./lib -lhello\n#include \"./lib/hello.h\"\n*/\nimport \"C\"\n```\n\nYou must also pass the `-ldflags` option to `go build` (see [the `build-dynamic` target in\nthe `Makefile`](https://github.com/mediremi/rust-plus-golang/blob/97e8444573698bdf2c82316074b112f7d6209e13/Makefile#L12-L15)).\n\nSee [`main_dynamic.go`](main_dynamic.go)\n\n### Static library setup\n\nFor a static library, an additional `-ldl` `LDFLAGS` flag is sometimes\nnecessary. This flag will link the `dl` library.\n\n```go\n/*\n#cgo LDFLAGS: ./lib/libhello.a -ldl\n#include \"./lib/hello.h\"\n*/\nimport \"C\"\n```\n\n\u003e There should not be a newline between `*/` and `import \"C\"`.\n\nSee [the `build-static` target in the `Makefile`](https://github.com/mediremi/rust-plus-golang/blob/97e8444573698bdf2c82316074b112f7d6209e13/Makefile#L18-L21) and [`main_dynamic.go`](main_dynamic.go).\n\n## See also\n\n* [rustgo: Calling Rust from Go with near-zero overhead](https://words.filippo.io/rustgo/)\n* [Hooking Go from Rust](https://metalbear.co/blog/hooking-go-from-rust-hitchhikers-guide-to-the-go-laxy/)\n\n#### License\n\n\u003csup\u003e\nLicensed under the \u003ca href=\"LICENSE\"\u003eMIT license\u003c/a\u003e.\n\u003c/sup\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmediremi%2Frust-plus-golang","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmediremi%2Frust-plus-golang","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmediremi%2Frust-plus-golang/lists"}