4 ms·
How would you call foreign functions in C or C++ from Go? Is there something like a foreign function interface?
by fdb 15y ago
How would you call foreign functions in C or C++ from Go? Is there something like a foreign function interface?
- justincormack 15y agoYes there is a C ffi. Not that well documented last time I looked, but you can look at the source of the standard libraries for examples.
- jkn 15y agoFrom http://blog.golang.org/2011/03/c-go-cgo.html http://blog.golang.org/2011/03/c-go-cgo.html : package rand /* #include <stdlib.h> */ import "C" func Random() int { return int(C.random()) } func Seed(i int) { C.srandom(C.uint(i)) } So calling C functions from Go is very easy. However the other way is not supported, AFAIK.
- luriel 15y agoCalling Go from C is supported, but is not quite as straightforward. You can also use SWIG.
- genbattle 15y agoGo has utility called CGo that allows you to create interfaces to C libraries in Go. I've heard you can use some magic to get Go to talk to C++ by exporting functions back to C somehow (disclaimer: I'm not a C/C++ person).