3

I cythonized my python file to example.c and example.so I can export these files as a python module and access the function in python as testing.py:

import example
example.test()

this way i can easily import my cythonized python files in python. How can i similarly import these files in golang using cgo?

mohd.gadi
  • 495
  • 1
  • 7
  • 15
  • You can access cython functionality from C, and thus import it to cgo. It is what you have in mind? Please be more specific, what you what to achieve. – ead Aug 30 '18 at 11:32
  • I want to call example.test method in golang similar to how i called example.test method above in python – mohd.gadi Aug 30 '18 at 12:19
  • Are you aware, that for Cython code to work a Python-intepreter must be running. You will have to manage that somehow. It will not going to be as easy as "import example". – ead Aug 30 '18 at 12:35

1 Answers1

1

Cython code includes a bunch of wrapping to code to create a python module and translate to and from Python objects and C types.

You may not need any of that. For example, a very simple hello world in cgo could be just:

/* hello.c */
#include <stdio.h>

static void hello(void) {
    printf("hello world!\n");
}

And you can call that from GO with C code using special import and some include directive comments:

package main

// #include <hello.c>
import "C"

func main() {
    C.hello()
}

You can certainly try to just include the above "cythonized" C file but you'll also need to include the appropriate linker directives so that Go can compile all the Python runtime stuff into your Go binary.

In general, it's likely not what you're looking to do...

You might want to look at something like http://grump.io/ if you really want to call Python from Go.

stderr
  • 8,567
  • 1
  • 34
  • 50