Is there a way to write nifs in Go?


Hi all,

In Erlang and Elixir it is possible to implement functions in C/C++ by using the NIFs facility.

Is there a way to write NIFs in Go instead of C or C++?


Yes it is possible, but you have to expand a lot of macros by hand. You basically do not want to do that.


Hi NobbZ,

Thank you for your reply.

I am aware of the rustler library which makes it possible (maybe easy) to write NIFs in Rust.
Do you have pointers to where to start to build a similar library for GO?


I think it might be problematic since Go is a garbage collected language. I’m not sure there wouldn’t be strange interference between the two garbage collectors.


I did only a small test (the usual adder example), At least there wasn’t any interference, but I deleted all the code, it was impractical.


I said, you do not wan’t to do it yourself. Even in C there is better meta programming possible than in Go.

There is a lot of boilerplate to be done for each module and for every function in it. In Go you have to copy and paste (with very slight and subtile modifications) for every single function. All the things usually done by the C macros have to be done by you in go. At least with my level of knowledge.

So if I really wanted to create such a thing (which I explicitely do not want to do), I’d start with a tool with creates the boilerplate code.

If you want to do something in Go, make it a Port, if you want it to be a NIF do it in C, C++, or Rust.


@NobbZ and @michalmuskala Thank you for your feedback :slight_smile: