Add support for the Go programming language.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@12108 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Ian Lance Taylor 2010-06-10 01:13:31 +00:00
commit 5af2978f77
259 changed files with 16159 additions and 14 deletions

View file

@ -0,0 +1,15 @@
TOP = ../..
SWIG = $(TOP)/../preinst-swig
SRCS = example.c
TARGET = example
INTERFACE = example.i
all::
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go
clean::
$(MAKE) -f $(TOP)/Makefile go_clean
check: all
$(MAKE) -f $(TOP)/Makefile TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go_run

View file

@ -0,0 +1,18 @@
/* 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;
}

View file

@ -0,0 +1,48 @@
/* ----------------------------------------------------------------------------
* This file was automatically generated by SWIG (http://www.swig.org).
* Version 2.0.1
*
* This file is not intended to be easily readable and contains a number of
* coding conventions designed to improve portability and efficiency. Do not make
* changes to this file unless you know what you are doing--modify the SWIG
* interface file instead.
* ----------------------------------------------------------------------------- */
package example
type _swig_fnptr *byte
type _swig_memberptr *byte
func _swig_allocatememory(int) *byte
func _swig_internal_allocate(len int) *byte {
return _swig_allocatememory(len)
}
func _swig_allocatestring(*byte, int) string
func _swig_internal_makegostring(p *byte, l int) string {
return _swig_allocatestring(p, l)
}
func _swig_internal_gopanic(p *byte, l int) {
panic(_swig_allocatestring(p, l))
}
func Gcd(int, int) int
func _swig_wrap_Foo_set(float64)
func SetFoo(arg1 float64) {
_swig_wrap_Foo_set(arg1)
}
func GetFoo() float64
type SwigcptrVoid uintptr
type Void interface {
Swigcptr() uintptr;
}
func (p SwigcptrVoid) Swigcptr() uintptr {
return uintptr(p)
}

View file

@ -0,0 +1,7 @@
/* File : example.i */
%module example
%inline %{
extern int gcd(int x, int y);
extern double Foo;
%}

View file

@ -0,0 +1,126 @@
<html>
<head>
<title>SWIG:Examples:go:simple</title>
</head>
<body bgcolor="#ffffff">
<tt>SWIG/Examples/go/simple/</tt>
<hr>
<H2>Simple Go Example</H2>
<p>
This example illustrates how you can hook Go to a very simple C program containing
a function and a global variable.
<h2>The C Code</h2>
Suppose you have the following C code:
<blockquote>
<pre>
/* 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 &gt; 0) {
g = x;
x = y % x;
y = g;
}
return g;
}
</pre>
</blockquote>
<h2>The SWIG interface</h2>
Here is a simple SWIG interface file:
<blockquote>
<pre>
/* File: example.i */
%module example
extern int gcd(int x, int y);
extern double Foo;
</pre>
</blockquote>
<h2>Compilation</h2>
These are the instructions if you are using <tt>6g</tt>/<tt>8g</tt>
rather than <tt>gccgo</tt>.
<ol>
<li>Run <tt>swig -go <a href="example.i">example.i</a></tt>. This
will create the three
files <tt>example.go</tt>, <tt>example_gc.c</tt>,
and <tt>example_wrap.c</tt>.
<li>Compile <tt><a href="example.go">example.go</a></tt>
using <tt>6g</tt> or <tt>8g</tt>; e.g., <tt>6g example.go</tt>.
<li>Compile <tt><a href="example_gc.c">example_gc.c</a></tt>
using <tt>6c</tt> or <tt>8c</tt>; e.g., <tt>6c example_gc.c</tt>.
<li>Put the two object files together into an archive
named <tt>example.a</tt>; e.g., <tt>gopack grc example.a example.6
example_gc.6</tt>.
<li>Compile the <tt><a href="example_wrap.c">example_wrap.c</a></tt>
file using your standard C compiler with the <tt>-fpic</tt> option;
e.g., <tt>gcc -c -O -fpic example_wrap.c</tt>.
<li>Put the gcc compiled object file into a shared library;
e.g., <tt>gcc -shared -o example.so example_wrap.o</tt>.
<li>Compile the program which demonstrates how to use the library;
e.g., <tt>6g runme.go</tt>.
<li>Link the program; e.g., <tt>6l -o runme runme.6</tt>.
<li>Now you should have a program <tt>runme</tt>.
</ol>
<h2>Using the extension</h2>
The Go program which demonstrates calling the C functions from Go
is <a href="runme.go">runme.go</a>.
<h2>Key points</h2>
<ul>
<li>Use the <tt>import</tt> statement to load your extension module from Go. For example:
<blockquote>
<pre>
import "example"
</pre>
</blockquote>
<li>C functions work just like Go functions. However, the function
names are automatically capitalized in order to make the names
visible from other Go packages. For example:
<blockquote>
<pre>
g := example.Gcd(42,105)
</pre>
</blockquote>
(If there are name conflicts, you can use the <tt>%rename</tt>
directive in the .i file or the <tt>-rename</tt> option to Go to
rename one or the other symbol).
<li>C global variables are accessed using getter and setter
functions. The getter function is named <tt>Get</tt> followed by
the capitalized name of the C variable. The Setter function
uses <tt>Set</tt> instead of <tt>Get</tt>.
<blockquote>
<pre>
a = example.GetFoo()
</pre>
</blockquote>
</ul>
<hr>
</body>
</html>

View file

@ -0,0 +1,25 @@
package main
import (
"fmt"
"./example"
)
func main() {
// Call our gcd() function
x := 42
y := 105
g := example.Gcd(x, y)
fmt.Println("The gcd of", x, "and", y, "is", g)
// Manipulate the Foo global variable
// Output its current value
fmt.Println("Foo =", example.GetFoo())
// Change its value
example.SetFoo(3.1415926)
// See if the change took effect
fmt.Println("Foo =", example.GetFoo())
}