This example illustrates a couple of techniques for handling simple pointers in SWIG. The prototypical example is a C function that operates on pointers such as this:
void add(int *x, int *y, int *r) {
*r = *x + *y;
}
By default, SWIG wraps this function exactly as specified and creates
an interface that expects pointer objects for arguments. This only
works when there is a precise correspondence between the C type and
some Go type.
and from Go you would use pointers like this:%include cpointer.i %pointer_functions(int, intp);
a := example.New_intp()
b := example.New_intp()
c := example.New_intp()
Intp_Assign(a, 37)
Intp_Assign(b, 42)
fmt.Println(" a =", a)
fmt.Println(" b =", b)
fmt.Println(" c =", c)
// Call the add() function with some pointers
example.Add(a,b,c)
// Now get the result
res := example.Intp_value(c)
fmt.Println(" 37 + 42 =", res)
// Clean up the pointers
example.Delete_intp(a)
example.Delete_intp(b)
example.Delete_intp(c)
And in a Go program:%include "typemaps.i" void add(int *INPUT, int *INPUT, int *OUTPUT);
r := []int{0}
example.Sub(37,42,r)
fmt.Println("Result =", r[0])
Needless to say, this is substantially easier although a bit unusual.
%include "typemaps.i"
%apply int *INPUT {int *x, int *y};
%apply int *OUTPUT {int *r};
void add(int *x, int *y, int *r);
void sub(int *x, int *y, int *r);
void mul(int *x, int *y, int *r);
... etc ...