Here’s a simple example of how to use a C library from Go. I made a small library called libfoo.
#include "libfoo.h"
int foo() {
return 1;
}
int foo();
Of course, you have to compile that as a shared object with GCC.
$ gcc -shared libfoo.c -o libfoo.so
Make sure you copy over the binary to /usr/lib/ and copy the header to /usr/include/.
package main
/*
#cgo LDFLAGS: -lfoo
#include <libfoo/libfoo.h>
*/
import "C"
import "fmt"
func main() {
fmt.Println("From libfoo: ", C.foo())
}
…and that’s it. I’m playing around with libguestfs and libvirt using Go. We’ll see how that Goes. The puns never end!