26

How do I use Go's "foreign function interface" to call out to a C function?

This interface is mentioned on the FAQ, but I cannot see it mentioned elsewhere in the docs.

ThorSummoner
  • 16,657
  • 15
  • 135
  • 147
pauldoo
  • 18,087
  • 20
  • 94
  • 116

2 Answers2

18

Check out this file from the Go repository. It shows how to wrap a C library in Go and has quite a few comments that explain the process.

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
Paul Wicks
  • 62,960
  • 55
  • 119
  • 146
  • 1
    That's a very well documented file! I had begun writing an answer but decided to check that file out and then deleted my unfinished answer after reading. – hannson Nov 18 '10 at 11:48
2

Here is an article that demonstrates the following with Cgo:

To lead with an example, here's a Go package that provides two functions - Random and Seed - that wrap C's rand and srand functions.

danriti
  • 897
  • 8
  • 9