Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call C from Go using the "foreign function interface"

Tags:

c

go

ffi

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.

like image 235
pauldoo Avatar asked Apr 29 '10 19:04

pauldoo


2 Answers

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.

like image 159
Paul Wicks Avatar answered Nov 14 '22 09:11

Paul Wicks


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.

like image 29
danriti Avatar answered Nov 14 '22 09:11

danriti