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
  • 14,302
  • 13
  • 126
  • 138
pauldoo
  • 17,349
  • 20
  • 91
  • 115

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
  • 13,194
  • 13
  • 82
  • 139
Paul Wicks
  • 59,431
  • 55
  • 117
  • 144
  • 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
  • 877
  • 8
  • 8