This example illustrates how you can hook Go to a very simple C program containing a function and a global variable.
/* File : example.c */
/* A global variable */
double Foo = 3.0;
/* Compute the greatest common divisor of positive integers */
int gcd(int x, int y) {
int g;
g = y;
while (x > 0) {
g = x;
x = y % x;
y = g;
}
return g;
}
/* File: example.i */ %module example extern int gcd(int x, int y); extern double Foo;
import "example"
(If there are name conflicts, you can use the %rename directive in the .i file or the -rename option to Go to rename one or the other symbol).g := example.Gcd(42,105)
a = example.GetFoo()