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++?

1 Like

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

1 Like

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?

1 Like

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.

1 Like

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.

1 Like

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

1 Like