Created
November 1, 2022 15:51
-
-
Save deniska/d3ad8280ddc51a7a6a5f90d49f85ba77 to your computer and use it in GitHub Desktop.
Call go code from python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
set -xe | |
go build -buildmode c-shared -o libhello.so hello.go | |
.venv/bin/python build_hello.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from cffi import FFI | |
ffibuilder = FFI() | |
ffibuilder.cdef(''' | |
void HelloFromGo(void); | |
''') | |
ffibuilder.set_source('_hello', ''' | |
#include "libhello.h" | |
''', extra_link_args=['-Wl,-rpath,$ORIGIN', '-L.', '-lhello'] | |
) | |
if __name__ == '__main__': | |
ffibuilder.compile(verbose=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ .venv/bin/python hello.py | |
Hello from go |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "C" | |
import ( | |
"fmt" | |
) | |
//export HelloFromGo | |
func HelloFromGo() { | |
fmt.Println("Hello from go") | |
} | |
func main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import _hello | |
_hello.lib.HelloFromGo() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment