I would like to know if I can use Go and Wolfram Language?

Example C

In[1]:= Needs["CCompilerDriver`GenericCCompiler`"]

In[2]:= greeter = CreateExecutable[StringJoin["#include <stdio.h>\n", "int main(){\n", " printf(\"Hello MinGW-w64 world.\\n\");\n", "}\n"], "hiworld", "Compiler" -> GenericCCompiler, "CompilerInstallation" -> "gcc/mingw/tdm/bin", "CompilerName" -> "g++.exe"]

Out[2]:= C:\Users\Casa\AppData\Roaming\Mathematica\SystemFiles\LibraryResources\Windows-x86-64\hiworld.exe

  • $\begingroup$ In theory, the way it should be done is that you compile your Go code to a C shared library. Then you should write some wrapper code in C to turn the Go data types into Wolfram Language data types. After that, you will be able to interface with your Go code (now in the form of a C library) with LibraryLink. I started doing this once, but unfortunately I got stuck because my C compiler stubbornly claimed that my Go compiled C library was not compiled for the right architecture. I haven't figured out why yet, otherwise I would have been able to give more specifics. $\endgroup$ – C. E. Jul 13 '18 at 3:17
  • $\begingroup$ One approach is what @C.E. mentioned, through LibraryLink. Another approach is through MathLink (also called WSTP). 1. Expose the MathLink API to Go. I believe Go has features for calling C libraries. 2. Understand how installable MathLink programs work. Work through an example in C. Look at the output of mprep and see how it can be reproduced in Go. 3. Now you should be ready to reproduce it in Go itself. $\endgroup$ – Szabolcs Jul 13 '18 at 9:36
  • $\begingroup$ I don't use Go, so I can't judge which method is better. With the LibraryLink method, it will be necessary to write Go functions which can be called from C. With the MathLink method, all you need is to use a C library from Go, but not the reverse (not call Go from C). I suspect the second method will be easier, but it also requires more learning (the MathLink API is much more complex than the LibraryLink one) $\endgroup$ – Szabolcs Jul 13 '18 at 9:37
  • $\begingroup$ @Szabolcs "it will be necessary to write Go functions which can be called from C" All you have to do is include the C package, write //export before the function definitions that you want to use in C, and then add the -buildmode=c-shared flag to the compiler. That's it, really. Then you get a C shared library that you can use in C code. You can see how this is done for Python here. $\endgroup$ – C. E. Jul 13 '18 at 10:25
  • $\begingroup$ Thank you for your help. I will test. $\endgroup$ – Marcelo Passos Jul 13 '18 at 22:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.