Git Product home page Git Product logo

rust-plus-golang's Introduction

Rust + Go(lang)

TL;DR: Call Rust code from Go using FFI


This repository shows how, by combining cgo and Rust's FFI capabilities, we can call Rust code from Go.

Two ways of achieving this are presented in this repository: with a dynamic library, and with a static library.

Running the example code

Run make run-all to see Rust + Go in action, building and running two binaries, one where the Rust code is compiled as a dynamic (sometimes also referred to as a 'shared') library, and one where it is compiled as a static library.

You should see the following output:

$ make run-all
   Compiling libc v0.2.132
   Compiling hello v0.1.0 (/home/user/rust-plus-golang/lib/hello)
    Finished release [optimized] target(s) in 0.1s
Hello world!
(this is code from the dynamic library)
    Finished release [optimized] target(s) in 0.00s
Hello world!
(this is code from the static library)

You will also find the binaries ./main_dynamic and ./main_static in your current working directory.

How it works

The Rust code is packaged up into a dynamic library and a static library, and the two Go binaries then call these using cgo.

You can do this for your own project

Andrew Oppenlander's article on creating a Rust dynamic library is a great introduction to this topic.

  1. Begin by creating a lib directory, where you will keep your Rust libraries.
  2. Then, create a C header file for your library. See the example hello.h.
  3. All that is left to do is to add some cgo-specific comments to your Go code. These comments tell cgo where to find the library and its headers.

Dynamic library setup

The following cgo comments are required to use a dynamic library:

/*
#cgo LDFLAGS: -L./lib -lhello
#include "./lib/hello.h"
*/
import "C"

You must also pass the -ldflags option to go build (see the build-dynamic target in the Makefile).

See main_dynamic.go

Static library setup

For a static library, an additional -ldl LDFLAGS flag is sometimes necessary. This flag will link the dl library.

/*
#cgo LDFLAGS: ./lib/libhello.a -ldl
#include "./lib/hello.h"
*/
import "C"

There should not be a newline between */ and import "C".

See the build-static target in the Makefile and main_dynamic.go.

See also

rust-plus-golang's People

Contributors

mediremi avatar vdods avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.