8

Is there any way to execute a Go function from C#? For Python I would use Ironpython for example.

I know that I could spawn a process to execute a Go script, but I don't really want to fallback to such a solution, if possible.

A Google search didn't reveal anything, so is there any way to do that using an API? Or do I have to fallback on processes?

Rakete1111
  • 47,013
  • 16
  • 123
  • 162

3 Answers3

5

I think Frank's answer is not right! Golang supports many build mode, and one is c-shared mode, it means you can use it like a c module. e.g: https://www.trs.ai/c%E8%B0%83%E7%94%A8golang%E4%BB%A3%E7%A0%81/

nathan.xu
  • 201
  • 4
  • 7
4

EDIT: my answer is now not true, since Go has been updated since I posted. this stackoverflow thread will help you solve your problem.

ORIGINAL ANSWER:

I don't think you're going to have much luck finding what you're looking for. Go is statically linked and C# doesn't support that.

From C#, calling a pre-compiled executable and reading the output is the easiest thing I can think of.

Frank Bryce
  • 8,076
  • 4
  • 38
  • 56
  • 9
    this question is not true anymore. go now can be dynamically build and can be called from languages like c, c++,c# etc.. – cahit beyaz Sep 28 '17 at 11:31
  • 2
    @cahitbeyaz could you add some links how to run golang code with taking into account CLR security constrains? – FLCL Jan 13 '18 at 14:34
  • @cahitbeyaz still can't find anything about it. – C4d May 22 '18 at 07:57
0

you can use grpc implement calls
grpc works across languages and platforms

Blue Joy
  • 335
  • 1
  • 7