Hacker News new | past | comments | ask | show | jobs | submit login

How would you call foreign functions in C or C++ from Go? Is there something like a foreign function interface?



From 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.


Calling Go from C is supported, but is not quite as straightforward. You can also use SWIG.


Go 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).


Yes 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.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: