+This chapter describes SWIG's support of Go. For more information on
+the Go programming language
+see golang.org.
+
+
+
20.1 Overview
+
+
+
+Go is a compiled language, not a scripting language. However, it does
+not support direct calling of functions written in C/C++. The cgo
+program may be used to generate wrappers to call C code from Go, but
+there is no convenient way to call C++ code. SWIG fills this gap.
+
+
+
+There are (at least) two different Go compilers. One is the gc
+compiler, normally invoked under the names 6g, 8g, or 5g. The other
+is the gccgo compiler, which is a frontend to the gcc compiler suite.
+The interface to C/C++ code is completely different for the two Go
+compilers. SWIG supports both, selected by a command line option.
+
+
+
+Because Go is a type-safe compiled language, SWIG's runtime type
+checking and runtime library are not used with Go. This should be
+borne in mind when reading the rest of the SWIG documentation.
+
+
+
20.2 Running SWIG with Go
+
+
+
+To generate Go code, use the -go option with SWIG. By
+default SWIG will generate code for the gc compilers. To generate
+code for gccgo, you should use the -gccgo option.
+
+
+
20.2.1 Additional Commandline Options
+
+
+
+These are the command line options for SWIG's GO module. They can
+also be seen by using:
+
+
+
+swig -go -help
+
+
+
+
+
Go specific options
+
+
+
+
-gccgo
+
Generate code for gccgo. The default is to generate code for
+ 6g/8g/6g.
+
+
+
+
-package <name>
+
Set the name of the Go package to <name>. The default
+ package name is the SWIG module name.
+
+
+
+
-go-prefix <prefix>
+
When generating code for gccgo, set the prefix to use. This
+ corresponds to the -fgo-prefix option to gccgo.
+
+
+
+
-long-type-size <s>
+
Set the size for the C/C++ type long. This controls
+ whether long is converted to the Go type int32
+ or int64. The <s> argument should be 32 or 64.
+
+
+
+
-rename <old>=<new>
+
Rename <old%gt; to <new> when processing the C/C++ code
+ and also the SWIG input file. This is a convenient way to rename
+ names in the C/C++ code which are the same expect for the first
+ letter, to avoid conflicts when applying the Go renaming rules
+ described below.
+
+
+
+
+
20.2.2 Go Output Files
+
+
+
When generating Go code, SWIG will generate the following
+ files:
+
+
+
+MODULE.go will contain the Go functions that your Go code will call.
+These functions will be wrappers for the C++ functions defined by your
+module. This file should, of course, be compiled with the Go
+compiler.
+
+MODULE_wrap.c or MODULE_wrap.cxx will contain C/C++ functions will be
+invoked by the Go wrapper code. This file should be compiled with the
+usual C or C++ compiler and linked into a shared library.
+
+MODULE_wrap.h will be generated if you use the directors feature. It
+provides a definition of the generated C++ director classes. It is
+generally not necessary to use this file, but in some special cases it
+may be helpful to include it in your code, compiled with the usual C
+or C++ compiler.
+
+If using the gc compiler, MODULE_gc.c will contain C code which should
+be compiled with the C compiler which part of the gc compiler: 6c, 8c,
+or 5c. It should then be combined with the compiled MODULE.go using
+gopack. This file will not be generated when using gccgo.
+
+
+
+A typical command sequence would look like this:
+
+By default, SWIG attempts to build a natural Go interface to your
+C/C++ code. However, the languages are somewhat different, so some
+modifications have to occur. This section briefly covers the
+essential aspects of this wrapping.
+
+
+
20.3.1 Go Package Name
+
+
+
+All Go source code lives in a package. The name of this package will
+default to the name of the module from SWIG's %module
+directive. You may override this by using SWIG's -package
+command line option.
+
+
+
20.3.2 Go Names
+
+
+
+In Go, a function is only visible outside the current package if the
+first letter of the name is uppercase. This is quite different from
+C/C++. Because of this, C/C++ names are modified when generating the
+Go interface: the first letter is forced to be uppercase if it is not
+already. This affects the names of functions, methods, variables,
+constants, enums, and classes.
+
+
+
+C/C++ variables are wrapped with setter and getter functions in Go.
+First the first letter of the variable name will be forced to
+uppercase, and then Get or Set will be prepended.
+For example, if the C/C++ variable is called var, then SWIG
+will define the functions GetVar and SetVar. If a
+variable is declared as const, or if
+SWIG's
+%immutable directive is used for the variable, then only
+the getter will be defined.
+
+
+
+C++ classes will be discussed further below. Here we'll note that the
+first letter of the class name will be forced to uppercase to give the
+name of a type in Go. A constructor will be named New
+followed by that name, and the destructor will be
+named Delete followed by that name.
+
+
+
20.3.3 Go Constants
+
+
+
+C/C++ constants created via #define or the %constant
+directive become Go constants, declared with a const
+declaration.
+
+
20.3.4 Go Enumerations
+
+
+
+C/C++ enumeration types will cause SWIG to define an integer type with
+the name of the enumeration (with first letter forced to uppercase as
+usual). The values of the enumeration will become variables in Go;
+code should avoid modifying those variables.
+
+
+
20.3.5 Go Classes
+
+
+
+Go has interfaces, methods and inheritance, but it does not have
+classes in the same sense as C++. This sections describes how SWIG
+represents C++ classes represented in Go.
+
+
+
+For a C++ class ClassName, SWIG will define two types in Go:
+an underlying type, which will just hold a pointer to the C++ type,
+and an interface type. The interface type will be
+named ClassName. SWIG will define a
+function NewClassName which will take any constructor
+arguments and return a value of the interface
+type ClassName. SWIG will also define a
+destructor DeleteClassName.
+
+
+
+SWIG will represent any methods of the C++ class as methods on the
+underlying type, and also as methods of the interface type. Thus C++
+methods may be invoked directly using the
+usual val.MethodName syntax. Public members of the C++ class
+will be given getter and setter functions defined as methods of the
+class.
+
+
+
+SWIG will represent static methods of C++ classes as ordinary Go
+functions. SWIG will use names like ClassName_MethodName.
+SWIG will give static members getter and setter functions with names
+like GetClassName_VarName.
+
+
+
+Given a value of the interface type, Go code can retrieve the pointer
+to the C++ type by calling the Swigcptr method. This will
+return a value of type SwigcptrClassName, which is just a
+name for uintptr. A Go type conversion can be used to
+convert this value to a different C++ type, but note that this
+conversion will not be type checked and is essentially equivalent
+to reinterpret_cast. This should only be used for very
+special cases, such as where C++ would use a dynamic_cast.
+
+
+
20.3.5.1 Go Class Inheritance
+
+
+
+C++ class inheritance is automatically represented in Go due to its
+use of interfaces. The interface for a child class will be a superset
+of the interface of its parent class. Thus a value of the child class
+type in Go may be passed to a function which expects the parent class.
+Doing the reverse will require an explicit type assertion, which will
+be checked dynamically.
+
+
+
20.3.6 Go Templates
+
+
+
+In order to use C++ templates in Go, you must tell SWIG to create
+wrappers for a particular template instantation. To do this, use
+the %template directive.
+
+
20.3.7 Go Director Classes
+
+
+
+SWIG's director feature permits a Go type to act as the subclass of a
+C++ class with virtual methods. This is complicated by the fact that
+C++ and Go define inheritance differently. In Go, structs can inherit
+methods via anonymous field embedding. However, when a method is
+called for an embedded struct, if that method calls any other methods,
+they are called for the embedded struct, not for the original type.
+Therefore, SWIG must use Go interfaces to represent C++ inheritance.
+
+
+
+In order to use the director feature in Go, you must define a type in
+your Go code. You must then add methods for the type. Define a
+method in Go for each C++ virtual function that you want to override.
+You must then create a value of your new type, and pass a pointer to
+it to the function NewDirectorClassName,
+where ClassName is the name of the C++ class. That will
+return a value of type ClassName.
+
+Any call in C++ code to the virtual function will wind up calling the
+method defined in Go. The Go code may of course call other methods on
+itself, and those methods may be defined either in Go or in C++.
+
+
+
20.3.8 Default Go primitive type mappings
+
+
+
+The following table lists the default type mapping from C/C++ to Go.
+This table will tell you which Go type to expect for a function which
+uses a given C/C++ type.
+
+
+
+
+
C/C++ type
+
Go type
+
+
+
+
bool
+
bool
+
+
+
+
char
+
byte
+
+
+
+
signed char
+
int8
+
+
+
+
unsigned char
+
byte
+
+
+
+
short
+
int16
+
+
+
+
unsigned short
+
uint16
+
+
+
+
int
+
int
+
+
+
+
unsigned int
+
uint
+
+
+
+
long
+
int32 or int64, depending on -long-type-size
+
+
+
+
unsigned long
+
uint32 or uint64, depending on -long-type-size
+
+
+
+
long long
+
int64
+
+
+
+
unsigned long long
+
uint64
+
+
+
+
float
+
float32
+
+
+
+
double
+
float64
+
+
+
+
char * char []
+
string
+
+
+
+
+
+Note that SWIG wraps the C char type as a character. Pointers
+and arrays of this type are wrapped as strings. The signed
+char type can be used if you want to treat char as a
+signed number rather than a character. Also note that all const
+references to primitive types are treated as if they are passed by
+value.
+
+
+
+These type mappings are defined by the "go" typemap. You may change
+that typemap, or add new values, to control how C/C++ types are mapped
+into Go types.
+
+
+
+
diff --git a/Doc/Manual/SWIG.html b/Doc/Manual/SWIG.html
index b4f332b14..d523bee77 100644
--- a/Doc/Manual/SWIG.html
+++ b/Doc/Manual/SWIG.html
@@ -113,6 +113,7 @@ can be obtained by typing swig -help or swig
-clisp Generate CLISP wrappers
-cffi Generate CFFI wrappers
-csharp Generate C# wrappers
+-go Generate Go wrappers
-guile Generate Guile wrappers
-java Generate Java wrappers
-lua Generate Lua wrappers
diff --git a/Doc/Manual/Sections.html b/Doc/Manual/Sections.html
index e514f726d..8693adc07 100644
--- a/Doc/Manual/Sections.html
+++ b/Doc/Manual/Sections.html
@@ -36,6 +36,7 @@ Last update : SWIG-2.0.1 (in progress)
+This example illustrates how to use directors to implement C++
+callbacks in Go.
+
+
+
+Because Go and C++ use inheritance differently, you must call a
+different function to create a class which uses callbacks. Instead of
+calling the usual constructor function whose name is New
+followed by the capitalized name of the class, you call a function
+named NewDirector followed by the capitalized name of the
+class.
+
+
+
+The first argument to the NewDirector function is an instance
+of a type. The NewDirector function will return an interface
+value as usual. However, when calling any method on the returned
+value, the program will first check whether the value passed
+to NewDirector implements that method. If it does, the
+method will be called in Go. This is true whether the method is
+called from Go code or C++ code.
+
+
+
+Note that the Go code will be called with just the Go value, not the
+C++ value. If the Go code needs to call a C++ method on itself, you
+need to get a copy of the C++ object. This is typically done as
+follows:
+
+
+
+type Child struct { abi Parent }
+func (p *Child) ChildMethod() {
+ p.abi.ParentMethod()
+}
+func f() {
+ p := &Child{nil}
+ d := NewDirectorParent(p)
+ p.abi = d
+ ...
+}
+
+
+
+In other words, we first create the Go value. We pass that to
+the NewDirector function to create the C++ value; this C++
+value will be created with an association to the Go value. We then
+store the C++ value in the Go value, giving us the reverse
+association. That permits us to call parent methods from the child.
+
+
+
+
+To delete a director object, use the function DeleteDirector
+followed by the capitalized name of the class.
+
+This example illustrates the most primitive form of C++ class wrapping
+performed by SWIG. In this case, C++ classes are simply transformed
+into a collection of C-style functions that provide access to class
+members.
+
+
The C++ Code
+
+Suppose you have some C++ classes described by the following (and
+admittedly lame) header file:
+
+
+
+A simple SWIG interface for this can be built by simply grabbing the
+header file like this:
+
+
+
+/* File : example.i */
+%module example
+
+%{
+#include "example.h"
+%}
+
+/* Let's just grab the original header file here */
+%include "example.h"
+
+
+
+Note: when creating a C++ extension, you must run SWIG with
+the -c++ option like this:
+
+
+% swig -c++ -go example.i
+
+
+
+
A sample Go script
+
+See example.go for a program that calls the
+C++ functions from Go.
+
+
Key points
+
+
+
To create a new object, you call a constructor like this:
+
+
+
+c := example.NewCircle(10.0)
+
+
+
+The name of the constructor is New followed by the name of
+the class, capitalized.
+
+
+
+The constructor returns a value of interface type. The methods of the
+interface will be the methods of the C++ class, plus member accessor
+functions.
+
+
+
To access member data, a pair of accessor methods are used. For
+example:
+
+
+
+c.SetX(15) # Set member data
+x := c.GetX() # Get member data.
+
+
+
+These are methods on the type returned by the constructor. The getter
+is named Get followed by the name of the member,
+capitalized. The setter is similar but uses Set.
+
+
+
To invoke a member function, you simply do this
+
+
+
+fmt.Println("The area is", example.c.Area())
+
+
+
+
To invoke a destructor, simply do this
+
+
+
+example.DeleteShape(c) # Deletes a shape
+
+
+
+The name of the destructor is Delete followed by the name of
+the class, capitalized. (Note: destructors are currently not
+inherited. This might change later).
+
+
+
Static member variables are wrapped much like C global variables.
+For example:
+
+
+
+n := GetShapeNshapes() # Get a static data member
+SetShapeNshapes(13) # Set a static data member
+
+
+
+The name is Get or Set, followed by the name of the
+class, capitalized, followed by the name of the member, capitalized.
+
+
+
+
General Comments
+
+
+
This low-level interface is not the only way to handle C++ code.
+Director classes provide a much higher-level interface.
+
+
+
Because C++ and Go implement inheritance quite differently, you
+can not simply upcast an object in Go code when using multiple
+inheritance. When using only single inheritance, you can simply pass
+a class to a function expecting a parent class. When using multiple
+inheritance, you have to call an automatically generated getter
+function named Get followed by the capitalized name of the
+immediate parent. This will return the same object converted to the
+ parent class.
+
+
+
+Overloaded methods should normally work. However, when calling an
+overloaded method you must explicitly convert constants to the
+expected type when it is not int or float. In
+particular, a floating point constant will default to
+type float, but C++ functions typically expect the C++
+type double which is equivalent to the Go
+type float64 So calling an overloaded method with a floating
+point constant typically requires an explicit conversion
+to float64.
+
+
+
Namespaces are not supported in any very coherent way.
+
+
+
+
+
+
diff --git a/Examples/go/class/runme.go b/Examples/go/class/runme.go
new file mode 100644
index 000000000..ff64bb4be
--- /dev/null
+++ b/Examples/go/class/runme.go
@@ -0,0 +1,63 @@
+// This example illustrates how C++ classes can be used from Go using SWIG.
+
+package main
+
+import (
+ "fmt"
+ . "./example"
+)
+
+func main() {
+ // ----- Object creation -----
+
+ fmt.Println("Creating some objects:")
+ c := NewCircle(10)
+ fmt.Println(" Created circle", c)
+ s := NewSquare(10)
+ fmt.Println(" Created square", s)
+
+ // ----- Access a static member -----
+
+ fmt.Println("\nA total of", GetShapeNshapes(), "shapes were created")
+
+ // ----- Member data access -----
+
+ // Notice how we can do this using functions specific to
+ // the 'Circle' class.
+ c.SetX(20)
+ c.SetY(30)
+
+ // Now use the same functions in the base class
+ var shape Shape = s
+ shape.SetX(-10)
+ shape.SetY(5)
+
+ fmt.Println("\nHere is their current position:")
+ fmt.Println(" Circle = (", c.GetX(), " ", c.GetY(), ")")
+ fmt.Println(" Square = (", s.GetX(), " ", s.GetY(), ")")
+
+ // ----- Call some methods -----
+
+ fmt.Println("\nHere are some properties of the shapes:")
+ shapes := []Shape{c, s}
+ for i := 0; i < len(shapes); i++ {
+ fmt.Println(" ", shapes[i])
+ fmt.Println(" area = ", shapes[i].Area())
+ fmt.Println(" perimeter = ", shapes[i].Perimeter())
+ }
+
+ // Notice how the area() and perimeter() functions really
+ // invoke the appropriate virtual method on each object.
+
+ // ----- Delete everything -----
+
+ fmt.Println("\nGuess I'll clean up now")
+
+ // Note: this invokes the virtual destructor
+ // You could leave this to the garbage collector
+ DeleteCircle(c)
+ DeleteSquare(s)
+
+ fmt.Println(GetShapeNshapes(), " shapes remain")
+ fmt.Println("Goodbye")
+}
diff --git a/Examples/go/constants/Makefile b/Examples/go/constants/Makefile
new file mode 100644
index 000000000..71d4d73a4
--- /dev/null
+++ b/Examples/go/constants/Makefile
@@ -0,0 +1,16 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+CXXSRCS =
+TARGET = example
+INTERFACE = example.i
+SWIGOPT =
+
+all::
+ $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
+ SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile go_clean
+
+check: all
+ $(MAKE) -f $(TOP)/Makefile TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go_run
diff --git a/Examples/go/constants/example.go b/Examples/go/constants/example.go
new file mode 100644
index 000000000..0e5e66418
--- /dev/null
+++ b/Examples/go/constants/example.go
@@ -0,0 +1,44 @@
+/* ----------------------------------------------------------------------------
+ * 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))
+}
+
+const ICONST int = 42
+const FCONST float64 = 2.1828
+const CCONST byte = 'x'
+func _swig_getCCONST2() byte
+var CCONST2 byte = _swig_getCCONST2()
+const SCONST string = "Hello World"
+func _swig_getSCONST2() string
+var SCONST2 string = _swig_getSCONST2()
+func _swig_getEXPR() float64
+var EXPR float64 = _swig_getEXPR()
+const Iconst int = 37
+const Fconst float64 = 3.14
+
diff --git a/Examples/go/constants/example.i b/Examples/go/constants/example.i
new file mode 100644
index 000000000..daca042df
--- /dev/null
+++ b/Examples/go/constants/example.i
@@ -0,0 +1,25 @@
+/* File : example.i */
+%module example
+
+/* A few preprocessor macros */
+
+#define ICONST 42
+#define FCONST 2.1828
+#define CCONST 'x'
+#define CCONST2 '\n'
+#define SCONST "Hello World"
+#define SCONST2 "\"Hello World\""
+
+/* This should work just fine */
+#define EXPR ICONST + 3*(FCONST)
+
+/* This shouldn't do anything */
+#define EXTERN extern
+
+/* Neither should this (BAR isn't defined) */
+#define FOO (ICONST + BAR)
+
+/* The following directives also produce constants */
+
+%constant int iconst = 37;
+%constant double fconst = 3.14;
diff --git a/Examples/go/constants/index.html b/Examples/go/constants/index.html
new file mode 100644
index 000000000..b1397ab2d
--- /dev/null
+++ b/Examples/go/constants/index.html
@@ -0,0 +1,55 @@
+
+
+SWIG:Examples:go:constants
+
+
+
+
+SWIG/Examples/go/constants/
+
+
+
Wrapping C Constants
+
+
+When SWIG encounters C preprocessor macros and C declarations that
+look like constants, it creates a Go constant with an identical value.
+Click here to see a SWIG interface with some
+constant declarations in it.
+
+
+
Accessing Constants from Go
+Click here for
+the section on constants in the SWIG and Go documentation.
+
+
+Click here to see a Go program that prints out
+the values of the constants contained in the above file.
+
Key points
+
+
All names are capitalized to make them visible.
+
The values of preprocessor macros are converted into Go constants.
+
C string literals such as "Hello World" are converted into Go strings.
+
Macros that are not fully defined are simply ignored. For example:
+
+
+#define EXTERN extern
+
+
+is ignored because SWIG has no idea what type of variable this would be.
+
+
+
Expressions are allowed provided that all of their components are
+defined. Otherwise, the constant is ignored.
+
+
Certain C declarations involving 'const' are also turned into Go
+constants.
+
The constants that appear in a SWIG interface file do not have to
+appear in any sort of matching C source file since the creation of a
+constant does not require linkage to a stored value (i.e., a value
+held in a C global variable or memory location).
+
+
+
+
+
+
diff --git a/Examples/go/constants/runme.go b/Examples/go/constants/runme.go
new file mode 100644
index 000000000..78a047e20
--- /dev/null
+++ b/Examples/go/constants/runme.go
@@ -0,0 +1,18 @@
+package main
+
+import (
+ "fmt"
+ "./example"
+)
+
+func main() {
+ fmt.Println("ICONST = ", example.ICONST, " (should be 42)")
+ fmt.Println("FCONST = ", example.FCONST, " (should be 2.1828)")
+ fmt.Printf("CCONST = %c (should be 'x')\n", example.CCONST)
+ fmt.Printf("CCONST2 = %c(this should be on a new line)\n", example.CCONST2)
+ fmt.Println("SCONST = ", example.SCONST, " (should be 'Hello World')")
+ fmt.Println("SCONST2 = ", example.SCONST2, " (should be '\"Hello World\"')")
+ fmt.Println("EXPR = ", example.EXPR, " (should be 48.5484)")
+ fmt.Println("iconst = ", example.Iconst, " (should be 37)")
+ fmt.Println("fconst = ", example.Fconst, " (should be 3.14)")
+}
diff --git a/Examples/go/enum/Makefile b/Examples/go/enum/Makefile
new file mode 100644
index 000000000..9dc8b8851
--- /dev/null
+++ b/Examples/go/enum/Makefile
@@ -0,0 +1,18 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+CXXSRCS = example.cxx
+TARGET = example
+INTERFACE = example.i
+SWIGOPT =
+
+all:: go
+
+go::
+ $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
+ SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go_cpp
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile go_clean
+
+check: all
+ $(MAKE) -f $(TOP)/Makefile TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go_run
diff --git a/Examples/go/enum/example.cxx b/Examples/go/enum/example.cxx
new file mode 100644
index 000000000..df7bb6328
--- /dev/null
+++ b/Examples/go/enum/example.cxx
@@ -0,0 +1,37 @@
+/* File : example.cxx */
+
+#include "example.h"
+#include
+
+void Foo::enum_test(speed s) {
+ if (s == IMPULSE) {
+ printf("IMPULSE speed\n");
+ } else if (s == WARP) {
+ printf("WARP speed\n");
+ } else if (s == LUDICROUS) {
+ printf("LUDICROUS speed\n");
+ } else {
+ printf("Unknown speed\n");
+ }
+}
+
+void enum_test(color c, Foo::speed s) {
+ if (c == RED) {
+ printf("color = RED, ");
+ } else if (c == BLUE) {
+ printf("color = BLUE, ");
+ } else if (c == GREEN) {
+ printf("color = GREEN, ");
+ } else {
+ printf("color = Unknown color!, ");
+ }
+ if (s == Foo::IMPULSE) {
+ printf("speed = IMPULSE speed\n");
+ } else if (s == Foo::WARP) {
+ printf("speed = WARP speed\n");
+ } else if (s == Foo::LUDICROUS) {
+ printf("speed = LUDICROUS speed\n");
+ } else {
+ printf("speed = Unknown speed!\n");
+ }
+}
diff --git a/Examples/go/enum/example.go b/Examples/go/enum/example.go
new file mode 100644
index 000000000..4653ab57a
--- /dev/null
+++ b/Examples/go/enum/example.go
@@ -0,0 +1,93 @@
+/* ----------------------------------------------------------------------------
+ * 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))
+}
+
+type Color int
+func _swig_getRED() Color
+var RED Color = _swig_getRED()
+func _swig_getBLUE() Color
+var BLUE Color = _swig_getBLUE()
+func _swig_getGREEN() Color
+var GREEN Color = _swig_getGREEN()
+type SwigcptrFoo uintptr
+
+func (p SwigcptrFoo) Swigcptr() uintptr {
+ return (uintptr)(p)
+}
+
+func (p SwigcptrFoo) SwigIsFoo() {
+}
+
+func _swig_wrap_new_Foo() SwigcptrFoo
+
+func NewFoo() Foo {
+ return _swig_wrap_new_Foo()
+}
+
+type FooSpeed int
+func _swig_getFoo_IMPULSE_Foo() FooSpeed
+var FooIMPULSE FooSpeed = _swig_getFoo_IMPULSE_Foo()
+func _swig_getFoo_WARP_Foo() FooSpeed
+var FooWARP FooSpeed = _swig_getFoo_WARP_Foo()
+func _swig_getFoo_LUDICROUS_Foo() FooSpeed
+var FooLUDICROUS FooSpeed = _swig_getFoo_LUDICROUS_Foo()
+func _swig_wrap_Foo_enum_test(SwigcptrFoo, FooSpeed)
+
+func (arg1 SwigcptrFoo) Enum_test(arg2 FooSpeed) {
+ _swig_wrap_Foo_enum_test(arg1, arg2)
+}
+
+func _swig_wrap_delete_Foo(uintptr)
+
+func DeleteFoo(arg1 Foo) {
+ _swig_wrap_delete_Foo(arg1.Swigcptr())
+}
+
+type Foo interface {
+ Swigcptr() uintptr
+ SwigIsFoo()
+ Enum_test(arg2 FooSpeed)
+}
+
+func _swig_wrap_enum_test(Color, FooSpeed)
+
+func Enum_test(arg1 Color, arg2 FooSpeed) {
+ _swig_wrap_enum_test(arg1, arg2)
+}
+
+
+type SwigcptrVoid uintptr
+type Void interface {
+ Swigcptr() uintptr;
+}
+func (p SwigcptrVoid) Swigcptr() uintptr {
+ return uintptr(p)
+}
+
diff --git a/Examples/go/enum/example.h b/Examples/go/enum/example.h
new file mode 100644
index 000000000..9119cd9fc
--- /dev/null
+++ b/Examples/go/enum/example.h
@@ -0,0 +1,13 @@
+/* File : example.h */
+
+enum color { RED, BLUE, GREEN };
+
+class Foo {
+ public:
+ Foo() { }
+ enum speed { IMPULSE=10, WARP=20, LUDICROUS=30 };
+ void enum_test(speed s);
+};
+
+void enum_test(color c, Foo::speed s);
+
diff --git a/Examples/go/enum/example.i b/Examples/go/enum/example.i
new file mode 100644
index 000000000..23ee8a822
--- /dev/null
+++ b/Examples/go/enum/example.i
@@ -0,0 +1,11 @@
+/* File : example.i */
+%module example
+
+%{
+#include "example.h"
+%}
+
+/* Let's just grab the original header file here */
+
+%include "example.h"
+
diff --git a/Examples/go/enum/index.html b/Examples/go/enum/index.html
new file mode 100644
index 000000000..868efe6ad
--- /dev/null
+++ b/Examples/go/enum/index.html
@@ -0,0 +1,42 @@
+
+
+SWIG:Examples:go:enum
+
+
+
+
+
+SWIG/Examples/go/enum/
+
+
+
Wrapping enumerations
+
+
+This example tests SWIG's ability to wrap enumerations.
+
+
+Enum values are expressed as constants or variables in GO.
+
+If the enum is named, then that name, capitalized, as defined as a new
+type name for int. All the enum values will be defined to
+have that type.
+
+If the enum is declared at global level, then the name in Go is simply
+the enum value, capitalized.
+
+If the enum is declared within a C++ class or struct, then the name in
+Go is the capitalized name of the class or struct followed by the
+capitalized name of the enum value.
+
+
+
+
+
diff --git a/Examples/go/extend/runme.go b/Examples/go/extend/runme.go
new file mode 100644
index 000000000..796c5ce68
--- /dev/null
+++ b/Examples/go/extend/runme.go
@@ -0,0 +1,77 @@
+// This file illustrates the cross language polymorphism using directors.
+
+package main
+
+import (
+ "fmt"
+ . "./example"
+)
+
+type CEO struct{}
+
+func (p *CEO) GetPosition() string {
+ return "CEO"
+}
+
+func main() {
+ // Create an instance of CEO, a class derived from the Go
+ // proxy of the underlying C++ class. The calls to getName()
+ // and getPosition() are standard, the call to getTitle() uses
+ // the director wrappers to call CEO.getPosition().
+
+ e := NewDirectorManager(new(CEO), "Alice")
+ fmt.Println(e.GetName(), " is a ", e.GetPosition())
+ fmt.Println("Just call her \"", e.GetTitle(), "\"")
+ fmt.Println("----------------------")
+
+
+ // Create a new EmployeeList instance. This class does not
+ // have a C++ director wrapper, but can be used freely with
+ // other classes that do.
+
+ list := NewEmployeeList()
+
+ // EmployeeList owns its items, so we must surrender ownership
+ // of objects we add.
+ // e.DisownMemory()
+ list.AddEmployee(e)
+ fmt.Println("----------------------")
+
+ // Now we access the first four items in list (three are C++
+ // objects that EmployeeList's constructor adds, the last is
+ // our CEO). The virtual methods of all these instances are
+ // treated the same. For items 0, 1, and 2, all methods
+ // resolve in C++. For item 3, our CEO, GetTitle calls
+ // GetPosition which resolves in Go. The call to GetPosition
+ // is slightly different, however, because of the overidden
+ // GetPosition() call, since now the object reference has been
+ // "laundered" by passing through EmployeeList as an
+ // Employee*. Previously, Go resolved the call immediately in
+ // CEO, but now Go thinks the object is an instance of class
+ // Employee. So the call passes through the Employee proxy
+ // class and on to the C wrappers and C++ director, eventually
+ // ending up back at the Java CEO implementation of
+ // getPosition(). The call to GetTitle() for item 3 runs the
+ // C++ Employee::getTitle() method, which in turn calls
+ // GetPosition(). This virtual method call passes down
+ // through the C++ director class to the Java implementation
+ // in CEO. All this routing takes place transparently.
+
+ fmt.Println("(position, title) for items 0-3:")
+
+ fmt.Println(" ", list.Get_item(0).GetPosition(), ", \"", list.Get_item(0).GetTitle(), "\"")
+ fmt.Println(" ", list.Get_item(1).GetPosition(), ", \"", list.Get_item(1).GetTitle(), "\"")
+ fmt.Println(" ", list.Get_item(2).GetPosition(), ", \"", list.Get_item(2).GetTitle(), "\"")
+ fmt.Println(" ", list.Get_item(3).GetPosition(), ", \"", list.Get_item(3).GetTitle(), "\"")
+ fmt.Println("----------------------")
+
+ // Time to delete the EmployeeList, which will delete all the
+ // Employee* items it contains. The last item is our CEO,
+ // which gets destroyed as well.
+ DeleteEmployeeList(list)
+ fmt.Println("----------------------")
+
+ // All done.
+
+ fmt.Println("Go exit")
+}
diff --git a/Examples/go/funcptr/Makefile b/Examples/go/funcptr/Makefile
new file mode 100644
index 000000000..b0aa9c970
--- /dev/null
+++ b/Examples/go/funcptr/Makefile
@@ -0,0 +1,18 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+SRCS = example.c
+TARGET = example
+INTERFACE = example.i
+SWIGOPT =
+
+all:: go
+
+go::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile go_clean
+
+check: all
+ $(MAKE) -f $(TOP)/Makefile TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go_run
diff --git a/Examples/go/funcptr/example.c b/Examples/go/funcptr/example.c
new file mode 100644
index 000000000..5c4a3dabf
--- /dev/null
+++ b/Examples/go/funcptr/example.c
@@ -0,0 +1,19 @@
+/* File : example.c */
+
+int do_op(int a, int b, int (*op)(int,int)) {
+ return (*op)(a,b);
+}
+
+int add(int a, int b) {
+ return a+b;
+}
+
+int sub(int a, int b) {
+ return a-b;
+}
+
+int mul(int a, int b) {
+ return a*b;
+}
+
+int (*funcvar)(int,int) = add;
diff --git a/Examples/go/funcptr/example.go b/Examples/go/funcptr/example.go
new file mode 100644
index 000000000..b059bae8d
--- /dev/null
+++ b/Examples/go/funcptr/example.go
@@ -0,0 +1,54 @@
+/* ----------------------------------------------------------------------------
+ * 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 Do_op(int, int, _swig_fnptr) int
+func _swig_getADD() _swig_fnptr
+var ADD _swig_fnptr = _swig_getADD()
+func _swig_getSUB() _swig_fnptr
+var SUB _swig_fnptr = _swig_getSUB()
+func _swig_getMUL() _swig_fnptr
+var MUL _swig_fnptr = _swig_getMUL()
+func _swig_wrap_funcvar_set(_swig_fnptr)
+
+func SetFuncvar(arg1 _swig_fnptr) {
+ _swig_wrap_funcvar_set(arg1)
+}
+
+func GetFuncvar() _swig_fnptr
+
+type SwigcptrVoid uintptr
+type Void interface {
+ Swigcptr() uintptr;
+}
+func (p SwigcptrVoid) Swigcptr() uintptr {
+ return uintptr(p)
+}
+
diff --git a/Examples/go/funcptr/example.h b/Examples/go/funcptr/example.h
new file mode 100644
index 000000000..9936e24fc
--- /dev/null
+++ b/Examples/go/funcptr/example.h
@@ -0,0 +1,9 @@
+/* file: example.h */
+
+extern int do_op(int,int, int (*op)(int,int));
+extern int add(int,int);
+extern int sub(int,int);
+extern int mul(int,int);
+
+extern int (*funcvar)(int,int);
+
diff --git a/Examples/go/funcptr/example.i b/Examples/go/funcptr/example.i
new file mode 100644
index 000000000..8b3bef678
--- /dev/null
+++ b/Examples/go/funcptr/example.i
@@ -0,0 +1,16 @@
+/* File : example.i */
+%module example
+%{
+#include "example.h"
+%}
+
+/* Wrap a function taking a pointer to a function */
+extern int do_op(int a, int b, int (*op)(int, int));
+
+/* Now install a bunch of "ops" as constants */
+%constant int (*ADD)(int,int) = add;
+%constant int (*SUB)(int,int) = sub;
+%constant int (*MUL)(int,int) = mul;
+
+extern int (*funcvar)(int,int);
+
diff --git a/Examples/go/funcptr/index.html b/Examples/go/funcptr/index.html
new file mode 100644
index 000000000..4212260a9
--- /dev/null
+++ b/Examples/go/funcptr/index.html
@@ -0,0 +1,89 @@
+
+
+SWIG:Examples:go:funcptr
+
+
+
+
+
+SWIG/Examples/go/funcptr/
+
+
+
Pointers to Functions
+
+
+Okay, just what in the heck does SWIG do with a declaration like this?
+
+
+
+int do_op(int a, int b, int (*op)(int, int));
+
+
+
+Well, it creates a wrapper as usual. Of course, that does raise some
+questions about the third argument (the pointer to a function).
+
+
+In this case, SWIG will wrap the function pointer as it does for all
+other pointers. However, in order to actually call this function from
+a Go program, you will need to pass some kind of C function pointer
+object. In C, this is easy, you just supply a function name as an
+argument like this:
+
+
+
+/* Some callback function */
+int add(int a, int b) {
+ return a+b;
+}
+...
+int r = do_op(x,y,add);
+
+
+
+To make this work with SWIG, you will need to do a little extra work.
+Specifically, you need to create some function pointer objects using
+the %constant directive like this:
+
+
+
+%constant(int (*)(int,int)) ADD = add;
+
+
+
+Now, in a Go program, you would do this:
+
+
+
+int r = do_op(x,y, example.ADD)
+
+
+where example is the module name.
+
+
An Example
+
+Here are some files that illustrate this with a simple example:
+
+
The value of a function pointer must correspond to a function
+written in C or C++. It is not possible to pass an arbitrary Go
+function in as a substitute for a C function pointer.
+
+
+
+
+
+
+
+
+
+
diff --git a/Examples/go/funcptr/runme.go b/Examples/go/funcptr/runme.go
new file mode 100644
index 000000000..73ecbb805
--- /dev/null
+++ b/Examples/go/funcptr/runme.go
@@ -0,0 +1,25 @@
+package main
+
+import (
+ "fmt"
+ . "./example"
+)
+
+func main() {
+ a := 37
+ b := 42
+
+ // Now call our C function with a bunch of callbacks
+
+ fmt.Println("Trying some C callback functions")
+ fmt.Println(" a = ", a)
+ fmt.Println(" b = ", b)
+ fmt.Println(" ADD(a,b) = ", Do_op(a, b, ADD))
+ fmt.Println(" SUB(a,b) = ", Do_op(a, b, SUB))
+ fmt.Println(" MUL(a,b) = ", Do_op(a, b, MUL))
+
+ fmt.Println("Here is what the C callback function classes are called in Go")
+ fmt.Println(" ADD = ", ADD)
+ fmt.Println(" SUB = ", SUB)
+ fmt.Println(" MUL = ", MUL)
+}
diff --git a/Examples/go/index.html b/Examples/go/index.html
new file mode 100644
index 000000000..21dda21b5
--- /dev/null
+++ b/Examples/go/index.html
@@ -0,0 +1,89 @@
+
+
+SWIG:Examples:Go
+
+
+
+
SWIG Go Examples
+
+
+The following examples illustrate the use of SWIG with Go.
+
+
+
simple. A minimal example showing how SWIG can
+be used to wrap a C function, a global variable, and a constant.
+
constants. This shows how preprocessor macros and
+certain C declarations are turned into constants.
+
variables. An example showing how to access C global variables from Go.
+
To create a Go extension, SWIG is run with the following options:
+
+
+
+% swig -go interface.i
+
+
+
+
On Unix the compilation of examples is done using the
+file Example/Makefile. This makefile performs a manual
+module compilation which is platform specific. When using
+the 6g or 8g compiler, the steps look like this
+(GNU/Linux):
+
+
+
+The examples have been extensively tested on the following platforms:
+
+
+
GNU/Linux
+
+
+All of the examples were last tested with the following configuration
+(10 May 2010):
+
+
+
Ubuntu Hardy
+
gcc-4.2.4
+
+
+Your mileage may vary. If you experience a problem, please let us know by
+contacting us on the mailing lists.
+
+
diff --git a/Examples/go/multimap/Makefile b/Examples/go/multimap/Makefile
new file mode 100644
index 000000000..b0aa9c970
--- /dev/null
+++ b/Examples/go/multimap/Makefile
@@ -0,0 +1,18 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+SRCS = example.c
+TARGET = example
+INTERFACE = example.i
+SWIGOPT =
+
+all:: go
+
+go::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile go_clean
+
+check: all
+ $(MAKE) -f $(TOP)/Makefile TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go_run
diff --git a/Examples/go/multimap/example.c b/Examples/go/multimap/example.c
new file mode 100644
index 000000000..b8360fa8a
--- /dev/null
+++ b/Examples/go/multimap/example.c
@@ -0,0 +1,53 @@
+/* File : example.c */
+#include
+#include
+#include
+
+/* 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;
+}
+
+int gcdmain(int argc, char *argv[]) {
+ int x,y;
+ if (argc != 3) {
+ printf("usage: gcd x y\n");
+ return -1;
+ }
+ x = atoi(argv[1]);
+ y = atoi(argv[2]);
+ printf("gcd(%d,%d) = %d\n", x,y,gcd(x,y));
+ return 0;
+}
+
+int count(char *bytes, int len, char c) {
+ int i;
+ int count = 0;
+ for (i = 0; i < len; i++) {
+ if (bytes[i] == c) count++;
+ }
+ return count;
+}
+
+void capitalize(char *str, int len) {
+ int i;
+ for (i = 0; i < len; i++) {
+ str[i] = (char)toupper(str[i]);
+ }
+}
+
+void circle(double x, double y) {
+ double a = x*x + y*y;
+ if (a > 1.0) {
+ printf("Bad points %g, %g\n", x,y);
+ } else {
+ printf("Good points %g, %g\n", x,y);
+ }
+}
diff --git a/Examples/go/multimap/example.go b/Examples/go/multimap/example.go
new file mode 100644
index 000000000..59ed9eaad
--- /dev/null
+++ b/Examples/go/multimap/example.go
@@ -0,0 +1,55 @@
+/* ----------------------------------------------------------------------------
+ * 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 Gcdmain([]string) int
+func Count(string, byte) int
+func _swig_wrap_capitalize([]string)
+
+func Capitalize(arg1 []string) {
+ _swig_wrap_capitalize(arg1)
+}
+
+func _swig_wrap_circle(float64, float64)
+
+func Circle(arg1 float64, arg2 float64) {
+ _swig_wrap_circle(arg1, arg2)
+}
+
+
+type SwigcptrVoid uintptr
+type Void interface {
+ Swigcptr() uintptr;
+}
+func (p SwigcptrVoid) Swigcptr() uintptr {
+ return uintptr(p)
+}
+
diff --git a/Examples/go/multimap/example.i b/Examples/go/multimap/example.i
new file mode 100644
index 000000000..04d6eea20
--- /dev/null
+++ b/Examples/go/multimap/example.i
@@ -0,0 +1,110 @@
+/* File : example.i */
+%module example
+
+%{
+extern int gcd(int x, int y);
+extern int gcdmain(int argc, char *argv[]);
+extern int count(char *bytes, int len, char c);
+extern void capitalize (char *str, int len);
+extern void circle (double cx, double cy);
+extern int squareCubed (int n, int *OUTPUT);
+%}
+
+extern int gcd(int x, int y);
+
+%typemap(go) (int argc, char *argv[]) "[]string"
+
+%typemap(in) (int argc, char *argv[])
+%{
+ {
+ int i;
+ _gostring_* a;
+
+ $1 = $input.len;
+ a = (_gostring_*) $input.array;
+ $2 = (char **) malloc (($1 + 1) * sizeof (char *));
+ for (i = 0; i < $1; i++) {
+ _gostring_ *ps = &a[i];
+ $2[i] = (char *) ps->p;
+ }
+ $2[i] = NULL;
+ }
+%}
+
+%typemap(argout) (int argc, char *argv[]) "" /* override char *[] default */
+
+%typemap(freearg) (int argc, char *argv[])
+%{
+ free($2);
+%}
+
+extern int gcdmain(int argc, char *argv[]);
+
+%typemap(go) (char *bytes, int len) "string"
+
+%typemap(in) (char *bytes, int len)
+%{
+ $1 = $input.p;
+ $2 = $input.n;
+%}
+
+extern int count(char *bytes, int len, char c);
+
+/* This example shows how to wrap a function that mutates a c string. A one
+ * element Go string slice is used so that the string can be returned
+ * modified.
+ */
+
+%typemap(go) (char *str, int len) "[]string"
+
+%typemap(in) (char *str, int len)
+%{
+ {
+ _gostring_ *a;
+ char *p;
+ int n;
+
+ a = (_gostring_*) $input.array;
+ p = a[0].p;
+ n = a[0].n;
+ $1 = malloc(n + 1);
+ $2 = n;
+ memcpy($1, p, n);
+ }
+%}
+
+/* Return the mutated string as a modified element in the array. */
+%typemap(argout) (char *str, int len)
+%{
+ {
+ _gostring_ *a;
+
+ a = (_gostring_*) $input.array;
+ a[0] = _swig_makegostring($1, $2);
+ }
+%}
+
+%typemap(freearg) (char *str, int len)
+%{
+ free($1);
+%}
+
+extern void capitalize(char *str, int len);
+
+/* A multi-valued constraint. Force two arguments to lie
+ inside the unit circle */
+
+%typemap(check) (double cx, double cy)
+%{
+ {
+ double a = $1*$1 + $2*$2;
+ if (a > 1.0) {
+ _swig_gopanic("$1_name and $2_name must be in unit circle");
+ return;
+ }
+ }
+%}
+
+extern void circle(double cx, double cy);
+
+
diff --git a/Examples/go/multimap/runme.go b/Examples/go/multimap/runme.go
new file mode 100644
index 000000000..94c29127c
--- /dev/null
+++ b/Examples/go/multimap/runme.go
@@ -0,0 +1,26 @@
+package main
+
+import (
+ "fmt"
+ . "./example"
+)
+
+func main() {
+ // Call our gcd() function
+ x := 42
+ y := 105
+ g := Gcd(x, y)
+ fmt.Println("The gcd of ", x, " and ", y, " is ", g)
+
+ // Call the gcdmain() function
+ args := []string{"gcdmain", "42", "105"}
+ Gcdmain(args)
+
+ // Call the count function
+ fmt.Println(Count("Hello World", 'l'))
+
+ // Call the capitalize function
+ capitalizeMe := []string{"hello world"}
+ Capitalize(capitalizeMe)
+ fmt.Println(capitalizeMe[0])
+}
diff --git a/Examples/go/pointer/Makefile b/Examples/go/pointer/Makefile
new file mode 100644
index 000000000..b0aa9c970
--- /dev/null
+++ b/Examples/go/pointer/Makefile
@@ -0,0 +1,18 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+SRCS = example.c
+TARGET = example
+INTERFACE = example.i
+SWIGOPT =
+
+all:: go
+
+go::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile go_clean
+
+check: all
+ $(MAKE) -f $(TOP)/Makefile TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go_run
diff --git a/Examples/go/pointer/example.c b/Examples/go/pointer/example.c
new file mode 100644
index 000000000..b877d9a5b
--- /dev/null
+++ b/Examples/go/pointer/example.c
@@ -0,0 +1,16 @@
+/* File : example.c */
+
+void add(int *x, int *y, int *result) {
+ *result = *x + *y;
+}
+
+void sub(int *x, int *y, int *result) {
+ *result = *x - *y;
+}
+
+int divide(int n, int d, int *r) {
+ int q;
+ q = n/d;
+ *r = n - q*d;
+ return q;
+}
diff --git a/Examples/go/pointer/example.go b/Examples/go/pointer/example.go
new file mode 100644
index 000000000..567c41c32
--- /dev/null
+++ b/Examples/go/pointer/example.go
@@ -0,0 +1,68 @@
+/* ----------------------------------------------------------------------------
+ * 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 _swig_wrap_add(*int, *int, *int)
+
+func Add(arg1 *int, arg2 *int, arg3 *int) {
+ _swig_wrap_add(arg1, arg2, arg3)
+}
+
+func New_intp() *int
+func Copy_intp(int) *int
+func _swig_wrap_delete_intp(*int)
+
+func Delete_intp(arg1 *int) {
+ _swig_wrap_delete_intp(arg1)
+}
+
+func _swig_wrap_intp_assign(*int, int)
+
+func Intp_assign(arg1 *int, arg2 int) {
+ _swig_wrap_intp_assign(arg1, arg2)
+}
+
+func Intp_value(*int) int
+func _swig_wrap_sub(int, int, []int)
+
+func Sub(arg1 int, arg2 int, arg3 []int) {
+ _swig_wrap_sub(arg1, arg2, arg3)
+}
+
+func Divide(int, int, []int) int
+
+type SwigcptrVoid uintptr
+type Void interface {
+ Swigcptr() uintptr;
+}
+func (p SwigcptrVoid) Swigcptr() uintptr {
+ return uintptr(p)
+}
+
diff --git a/Examples/go/pointer/example.i b/Examples/go/pointer/example.i
new file mode 100644
index 000000000..a8ac79499
--- /dev/null
+++ b/Examples/go/pointer/example.i
@@ -0,0 +1,30 @@
+/* File : example.i */
+%module example
+
+%{
+extern void add(int *, int *, int *);
+extern void sub(int *, int *, int *);
+extern int divide(int, int, int *);
+%}
+
+/* This example illustrates a couple of different techniques
+ for manipulating C pointers */
+
+/* First we'll use the pointer library */
+extern void add(int *x, int *y, int *result);
+%include cpointer.i
+%pointer_functions(int, intp);
+
+/* Next we'll use some typemaps */
+
+%include typemaps.i
+extern void sub(int *INPUT, int *INPUT, int *OUTPUT);
+
+/* Next we'll use typemaps and the %apply directive */
+
+%apply int *OUTPUT { int *r };
+extern int divide(int n, int d, int *r);
+
+
+
+
diff --git a/Examples/go/pointer/index.html b/Examples/go/pointer/index.html
new file mode 100644
index 000000000..cfa8a3acc
--- /dev/null
+++ b/Examples/go/pointer/index.html
@@ -0,0 +1,143 @@
+
+
+SWIG:Examples:go:pointer
+
+
+
+
+SWIG/Examples/go/pointer/
+
+
+
Simple Pointer Handling
+
+
+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.
+
+
+
Other approaches
+
+
+
The SWIG pointer library provides a different, safer, way to
+ handle pointers. For example, in the interface file you would do
+ this:
+
+
+
+and from Go you would use pointers like this:
+
+
+
+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)
+
+
+
+
+
Use the SWIG typemap library. This library allows you to
+completely change the way arguments are processed by SWIG. For
+example:
+
+
+
+%include "typemaps.i"
+void add(int *INPUT, int *INPUT, int *OUTPUT);
+
+Needless to say, this is substantially easier although a bit unusual.
+
+
+
A final alternative is to use the typemaps library in combination
+with the %apply directive. This allows you to change the names of parameters
+that behave as input or output parameters. For example:
+
+
+
+%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 ...
+
+
+
+
+
+
Example
+
+The following example illustrates the use of these features for pointer
+extraction.
+
+
Since pointers are used for so many different things (arrays, output values,
+etc...) the complexity of pointer handling can be as complicated as you want to
+make it.
+
+
+
More documentation on the typemaps.i and cpointer.i library files can be
+found in the SWIG user manual. The files also contain documentation.
+
+
+
+
+
+
diff --git a/Examples/go/pointer/runme.go b/Examples/go/pointer/runme.go
new file mode 100644
index 000000000..9cbcda489
--- /dev/null
+++ b/Examples/go/pointer/runme.go
@@ -0,0 +1,47 @@
+package main
+
+import (
+ "fmt"
+ . "./example"
+)
+
+func main() {
+ // First create some objects using the pointer library.
+ fmt.Println("Testing the pointer library")
+ a := New_intp()
+ b := New_intp()
+ c := 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
+ Add(a, b, c)
+
+ // Now get the result
+ res := Intp_value(c)
+ fmt.Println(" 37 + 42 =", res)
+
+ // Clean up the pointers
+ Delete_intp(a)
+ Delete_intp(b)
+ Delete_intp(c)
+
+ // Now try the typemap library
+ // Now it is no longer necessary to manufacture pointers.
+ // Instead we use a single element array which in Java is modifiable.
+
+ fmt.Println("Trying the typemap library")
+ r := []int{0}
+ Sub(37, 42, r)
+ fmt.Println(" 37 - 42 = ", r[0])
+
+ // Now try the version with return value
+
+ fmt.Println("Testing return value")
+ q := Divide(42, 37, r)
+ fmt.Println(" 42/37 = ", q, " remainder ", r[0])
+}
diff --git a/Examples/go/reference/Makefile b/Examples/go/reference/Makefile
new file mode 100644
index 000000000..9dc8b8851
--- /dev/null
+++ b/Examples/go/reference/Makefile
@@ -0,0 +1,18 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+CXXSRCS = example.cxx
+TARGET = example
+INTERFACE = example.i
+SWIGOPT =
+
+all:: go
+
+go::
+ $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
+ SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go_cpp
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile go_clean
+
+check: all
+ $(MAKE) -f $(TOP)/Makefile TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go_run
diff --git a/Examples/go/reference/example.cxx b/Examples/go/reference/example.cxx
new file mode 100644
index 000000000..8a513bf49
--- /dev/null
+++ b/Examples/go/reference/example.cxx
@@ -0,0 +1,46 @@
+/* File : example.cxx */
+
+/* Deal with Microsoft's attempt at deprecating C standard runtime functions */
+#if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER)
+# define _CRT_SECURE_NO_DEPRECATE
+#endif
+
+#include "example.h"
+#include
+#include
+
+Vector operator+(const Vector &a, const Vector &b) {
+ Vector r;
+ r.x = a.x + b.x;
+ r.y = a.y + b.y;
+ r.z = a.z + b.z;
+ return r;
+}
+
+char *Vector::print() {
+ static char temp[512];
+ sprintf(temp,"Vector %p (%g,%g,%g)", this, x,y,z);
+ return temp;
+}
+
+VectorArray::VectorArray(int size) {
+ items = new Vector[size];
+ maxsize = size;
+}
+
+VectorArray::~VectorArray() {
+ delete [] items;
+}
+
+Vector &VectorArray::operator[](int index) {
+ if ((index < 0) || (index >= maxsize)) {
+ printf("Panic! Array index out of bounds.\n");
+ exit(1);
+ }
+ return items[index];
+}
+
+int VectorArray::size() {
+ return maxsize;
+}
+
diff --git a/Examples/go/reference/example.go b/Examples/go/reference/example.go
new file mode 100644
index 000000000..fb98f8a18
--- /dev/null
+++ b/Examples/go/reference/example.go
@@ -0,0 +1,126 @@
+/* ----------------------------------------------------------------------------
+ * 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))
+}
+
+type SwigcptrVector uintptr
+
+func (p SwigcptrVector) Swigcptr() uintptr {
+ return (uintptr)(p)
+}
+
+func (p SwigcptrVector) SwigIsVector() {
+}
+
+func _swig_wrap_new_Vector(float64, float64, float64) SwigcptrVector
+
+func NewVector(arg1 float64, arg2 float64, arg3 float64) Vector {
+ return _swig_wrap_new_Vector(arg1, arg2, arg3)
+}
+
+func _swig_wrap_delete_Vector(uintptr)
+
+func DeleteVector(arg1 Vector) {
+ _swig_wrap_delete_Vector(arg1.Swigcptr())
+}
+
+func _swig_wrap_Vector_print(SwigcptrVector) string
+
+func (arg1 SwigcptrVector) Print() string {
+ return _swig_wrap_Vector_print(arg1)
+}
+
+type Vector interface {
+ Swigcptr() uintptr
+ SwigIsVector()
+ Print() string
+}
+
+func _swig_wrap_addv(uintptr, uintptr) SwigcptrVector
+
+func Addv(arg1 Vector, arg2 Vector) Vector {
+ return _swig_wrap_addv(arg1.Swigcptr(), arg2.Swigcptr())
+}
+
+type SwigcptrVectorArray uintptr
+
+func (p SwigcptrVectorArray) Swigcptr() uintptr {
+ return (uintptr)(p)
+}
+
+func (p SwigcptrVectorArray) SwigIsVectorArray() {
+}
+
+func _swig_wrap_new_VectorArray(int) SwigcptrVectorArray
+
+func NewVectorArray(arg1 int) VectorArray {
+ return _swig_wrap_new_VectorArray(arg1)
+}
+
+func _swig_wrap_delete_VectorArray(uintptr)
+
+func DeleteVectorArray(arg1 VectorArray) {
+ _swig_wrap_delete_VectorArray(arg1.Swigcptr())
+}
+
+func _swig_wrap_VectorArray_size(SwigcptrVectorArray) int
+
+func (arg1 SwigcptrVectorArray) Size() int {
+ return _swig_wrap_VectorArray_size(arg1)
+}
+
+func _swig_wrap_VectorArray_get(SwigcptrVectorArray, int) SwigcptrVector
+
+func (arg1 SwigcptrVectorArray) Get(arg2 int) Vector {
+ return _swig_wrap_VectorArray_get(arg1, arg2)
+}
+
+func _swig_wrap_VectorArray_set(SwigcptrVectorArray, int, uintptr)
+
+func (arg1 SwigcptrVectorArray) Set(arg2 int, arg3 Vector) {
+ _swig_wrap_VectorArray_set(arg1, arg2, arg3.Swigcptr())
+}
+
+type VectorArray interface {
+ Swigcptr() uintptr
+ SwigIsVectorArray()
+ Size() int
+ Get(arg2 int) Vector
+ Set(arg2 int, arg3 Vector)
+}
+
+
+type SwigcptrVoid uintptr
+type Void interface {
+ Swigcptr() uintptr;
+}
+func (p SwigcptrVoid) Swigcptr() uintptr {
+ return uintptr(p)
+}
+
diff --git a/Examples/go/reference/example.h b/Examples/go/reference/example.h
new file mode 100644
index 000000000..4915adb1b
--- /dev/null
+++ b/Examples/go/reference/example.h
@@ -0,0 +1,26 @@
+/* File : example.h */
+
+class Vector {
+private:
+ double x,y,z;
+public:
+ Vector() : x(0), y(0), z(0) { };
+ Vector(double x, double y, double z) : x(x), y(y), z(z) { };
+ friend Vector operator+(const Vector &a, const Vector &b);
+ char *print();
+};
+
+class VectorArray {
+private:
+ Vector *items;
+ int maxsize;
+public:
+ VectorArray(int maxsize);
+ ~VectorArray();
+ Vector &operator[](int);
+ int size();
+};
+
+
+
+
diff --git a/Examples/go/reference/example.i b/Examples/go/reference/example.i
new file mode 100644
index 000000000..1cf19c82c
--- /dev/null
+++ b/Examples/go/reference/example.i
@@ -0,0 +1,42 @@
+/* File : example.i */
+
+/* This file has a few "typical" uses of C++ references. */
+
+%module example
+
+%{
+#include "example.h"
+%}
+
+class Vector {
+public:
+ Vector(double x, double y, double z);
+ ~Vector();
+ char *print();
+};
+
+/* This helper function calls an overloaded operator */
+%inline %{
+Vector addv(Vector &a, Vector &b) {
+ return a+b;
+}
+%}
+
+/* Wrapper around an array of vectors class */
+
+class VectorArray {
+public:
+ VectorArray(int maxsize);
+ ~VectorArray();
+ int size();
+
+ /* This wrapper provides an alternative to the [] operator */
+ %extend {
+ Vector &get(int index) {
+ return (*$self)[index];
+ }
+ void set(int index, Vector &a) {
+ (*$self)[index] = a;
+ }
+ }
+};
diff --git a/Examples/go/reference/index.html b/Examples/go/reference/index.html
new file mode 100644
index 000000000..5e8589349
--- /dev/null
+++ b/Examples/go/reference/index.html
@@ -0,0 +1,143 @@
+
+
+SWIG:Examples:go:reference
+
+
+
+
+
+SWIG/Examples/go/reference/
+
+
+
C++ Reference Handling
+
+
+This example tests SWIG's handling of C++ references. A reference in
+C++ is much like a pointer. Go represents C++ classes as pointers
+which are stored in interface values. Therefore, a reference to a
+class in C++ simply becomes an object of the class type in Go. For
+types which are not classes, a reference in C++ is represented as a
+pointer in Go.
+
+
Some examples
+
+References are most commonly used as function parameters. For
+example, you might have a function like this:
+
+
+
+In these cases, SWIG transforms everything into a pointer and creates
+a wrapper that looks like this in C++.
+
+
+
+Vector wrap_addv(Vector *a, Vector *b);
+
+
+
+or like this in Go:
+
+
+
+func Addv(arg1 Vector, arg2 Vector) Vector
+
+
+
+Occasionally, a reference is used as a return value of a function
+when the return result is to be used as an lvalue in an expression.
+The prototypical example is an operator like this:
+
+
+
+Vector &operator[](int index);
+
+
+
+or a method:
+
+
+
+Vector &get(int index);
+
+
+
+For functions returning references, a wrapper like this is created:
+
+
+
+The following header file contains some class
+definitions with some operators and use of references.
+
+
SWIG Interface
+
+SWIG does NOT support overloaded operators so it can not directly
+build an interface to the classes in the above file. However, a
+number of workarounds can be made. For example, an overloaded
+operator can be stuck behind a function call such as the addv
+function above. Array access can be handled with a pair of set/get
+functions like this:
+
+
+
+Click here to see a SWIG interface file with
+these additions.
+
+
Sample Go program
+
+Click here to see a Go program that manipulates
+some C++ references.
+
+
Notes:
+
+
+
C++ references primarily provide notational convenience for C++
+source code. However, Go only supports the 'x.a' notation so it
+doesn't much matter.
+
+
+
When a program returns a reference, a pointer is returned. Unlike
+return by value, memory is not allocated to hold the return result.
+
+
+
SWIG has particular trouble handling various combinations of
+references and pointers. This is side effect of an old parsing scheme
+and type representation that will be replaced in future versions.
+
+
+
+
+
+
diff --git a/Examples/go/reference/runme.go b/Examples/go/reference/runme.go
new file mode 100644
index 000000000..3683d6144
--- /dev/null
+++ b/Examples/go/reference/runme.go
@@ -0,0 +1,71 @@
+// This example illustrates the manipulation of C++ references in Java.
+
+package main
+
+import (
+ "fmt"
+ . "./example"
+)
+
+func main() {
+ fmt.Println("Creating some objects:")
+ a := NewVector(3, 4, 5)
+ b := NewVector(10, 11, 12)
+
+ fmt.Println(" Created ", a.Print())
+ fmt.Println(" Created ", b.Print())
+
+ // ----- Call an overloaded operator -----
+
+ // This calls the wrapper we placed around
+ //
+ // operator+(const Vector &a, const Vector &)
+ //
+ // It returns a new allocated object.
+
+ fmt.Println("Adding a+b")
+ c := Addv(a, b)
+ fmt.Println(" a+b = " + c.Print())
+
+ // Because addv returns a reference, Addv will return a
+ // pointer allocated using Go's memory allocator. That means
+ // that it will be freed by Go's garbage collector, and we can
+ // not use DeleteVector to release it.
+
+ c = nil
+
+ // ----- Create a vector array -----
+
+ fmt.Println("Creating an array of vectors")
+ va := NewVectorArray(10)
+ fmt.Println(" va = ", va)
+
+ // ----- Set some values in the array -----
+
+ // These operators copy the value of Vector a and Vector b to
+ // the vector array
+ va.Set(0, a)
+ va.Set(1, b)
+
+ va.Set(2, Addv(a, b))
+
+ // Get some values from the array
+
+ fmt.Println("Getting some array values")
+ for i := 0; i < 5; i++ {
+ fmt.Println(" va(", i, ") = ", va.Get(i).Print())
+ }
+
+ // Watch under resource meter to check on this
+ fmt.Println("Making sure we don't leak memory.")
+ for i := 0; i < 1000000; i++ {
+ c = va.Get(i % 10)
+ }
+
+ // ----- Clean up ----- This could be omitted. The garbage
+ // collector would then clean up for us.
+ fmt.Println("Cleaning up")
+ DeleteVectorArray(va)
+ DeleteVector(a)
+ DeleteVector(b)
+}
diff --git a/Examples/go/simple/Makefile b/Examples/go/simple/Makefile
new file mode 100644
index 000000000..e67fa8bb6
--- /dev/null
+++ b/Examples/go/simple/Makefile
@@ -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
diff --git a/Examples/go/simple/example.c b/Examples/go/simple/example.c
new file mode 100644
index 000000000..1c2af789c
--- /dev/null
+++ b/Examples/go/simple/example.c
@@ -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;
+}
+
+
diff --git a/Examples/go/simple/example.go b/Examples/go/simple/example.go
new file mode 100644
index 000000000..df0e70564
--- /dev/null
+++ b/Examples/go/simple/example.go
@@ -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)
+}
+
diff --git a/Examples/go/simple/example.i b/Examples/go/simple/example.i
new file mode 100644
index 000000000..24093b9bf
--- /dev/null
+++ b/Examples/go/simple/example.i
@@ -0,0 +1,7 @@
+/* File : example.i */
+%module example
+
+%inline %{
+extern int gcd(int x, int y);
+extern double Foo;
+%}
diff --git a/Examples/go/simple/index.html b/Examples/go/simple/index.html
new file mode 100644
index 000000000..82372ef37
--- /dev/null
+++ b/Examples/go/simple/index.html
@@ -0,0 +1,126 @@
+
+
+SWIG:Examples:go:simple
+
+
+
+
+
+SWIG/Examples/go/simple/
+
+
+
Simple Go Example
+
+
+This example illustrates how you can hook Go to a very simple C program containing
+a function and a global variable.
+
+
The C Code
+
+Suppose you have the following C code:
+
+
+
+/* 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;
+}
+
+
+
+
The SWIG interface
+
+Here is a simple SWIG interface file:
+
+
+
+/* File: example.i */
+%module example
+
+extern int gcd(int x, int y);
+extern double Foo;
+
+
+
+
Compilation
+
+These are the instructions if you are using 6g/8g
+rather than gccgo.
+
+
+
Run swig -go example.i. This
+ will create the three
+ files example.go, example_gc.c,
+ and example_wrap.c.
+
Compile example.go
+ using 6g or 8g; e.g., 6g example.go.
+
Compile example_gc.c
+ using 6c or 8c; e.g., 6c example_gc.c.
+
Put the two object files together into an archive
+ named example.a; e.g., gopack grc example.a example.6
+ example_gc.6.
+
Compile the example_wrap.c
+ file using your standard C compiler with the -fpic option;
+ e.g., gcc -c -O -fpic example_wrap.c.
+
Put the gcc compiled object file into a shared library;
+ e.g., gcc -shared -o example.so example_wrap.o.
+
Compile the program which demonstrates how to use the library;
+ e.g., 6g runme.go.
+
Link the program; e.g., 6l -o runme runme.6.
+
Now you should have a program runme.
+
+
+
Using the extension
+
+The Go program which demonstrates calling the C functions from Go
+is runme.go.
+
+
Key points
+
+
+
Use the import statement to load your extension module from Go. For example:
+
+
+import "example"
+
+
+
+
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:
+
+
+g := example.Gcd(42,105)
+
+
+
+(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).
+
+
C global variables are accessed using getter and setter
+ functions. The getter function is named Get followed by
+ the capitalized name of the C variable. The Setter function
+ uses Set instead of Get.
+
+
+a = example.GetFoo()
+
+
+
+
+
+
+
diff --git a/Examples/go/simple/runme.go b/Examples/go/simple/runme.go
new file mode 100644
index 000000000..c829ad21a
--- /dev/null
+++ b/Examples/go/simple/runme.go
@@ -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())
+}
diff --git a/Examples/go/template/Makefile b/Examples/go/template/Makefile
new file mode 100644
index 000000000..b9278b53e
--- /dev/null
+++ b/Examples/go/template/Makefile
@@ -0,0 +1,18 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+CXXSRCS =
+TARGET = example
+INTERFACE = example.i
+SWIGOPT =
+
+all:: go
+
+go::
+ $(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
+ SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go_cpp
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile go_clean
+
+check: all
+ $(MAKE) -f $(TOP)/Makefile TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go_run
diff --git a/Examples/go/template/example.go b/Examples/go/template/example.go
new file mode 100644
index 000000000..671b5c2ba
--- /dev/null
+++ b/Examples/go/template/example.go
@@ -0,0 +1,150 @@
+/* ----------------------------------------------------------------------------
+ * 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 Maxint(int, int) int
+func Maxdouble(float64, float64) float64
+type SwigcptrVecint uintptr
+
+func (p SwigcptrVecint) Swigcptr() uintptr {
+ return (uintptr)(p)
+}
+
+func (p SwigcptrVecint) SwigIsVecint() {
+}
+
+func _swig_wrap_new_vecint(int) SwigcptrVecint
+
+func NewVecint(arg1 int) Vecint {
+ return _swig_wrap_new_vecint(arg1)
+}
+
+func _swig_wrap_vecint_get(SwigcptrVecint, int) *int
+
+func (arg1 SwigcptrVecint) Get(arg2 int) *int {
+ return _swig_wrap_vecint_get(arg1, arg2)
+}
+
+func _swig_wrap_vecint_set(SwigcptrVecint, int, *int)
+
+func (arg1 SwigcptrVecint) Set(arg2 int, arg3 *int) {
+ _swig_wrap_vecint_set(arg1, arg2, arg3)
+}
+
+func _swig_wrap_vecint_getitem(SwigcptrVecint, int) int
+
+func (arg1 SwigcptrVecint) Getitem(arg2 int) int {
+ return _swig_wrap_vecint_getitem(arg1, arg2)
+}
+
+func _swig_wrap_vecint_setitem(SwigcptrVecint, int, int)
+
+func (arg1 SwigcptrVecint) Setitem(arg2 int, arg3 int) {
+ _swig_wrap_vecint_setitem(arg1, arg2, arg3)
+}
+
+func _swig_wrap_delete_vecint(uintptr)
+
+func DeleteVecint(arg1 Vecint) {
+ _swig_wrap_delete_vecint(arg1.Swigcptr())
+}
+
+type Vecint interface {
+ Swigcptr() uintptr
+ SwigIsVecint()
+ Get(arg2 int) *int
+ Set(arg2 int, arg3 *int)
+ Getitem(arg2 int) int
+ Setitem(arg2 int, arg3 int)
+}
+
+type SwigcptrVecdouble uintptr
+
+func (p SwigcptrVecdouble) Swigcptr() uintptr {
+ return (uintptr)(p)
+}
+
+func (p SwigcptrVecdouble) SwigIsVecdouble() {
+}
+
+func _swig_wrap_new_vecdouble(int) SwigcptrVecdouble
+
+func NewVecdouble(arg1 int) Vecdouble {
+ return _swig_wrap_new_vecdouble(arg1)
+}
+
+func _swig_wrap_vecdouble_get(SwigcptrVecdouble, int) *float64
+
+func (arg1 SwigcptrVecdouble) Get(arg2 int) *float64 {
+ return _swig_wrap_vecdouble_get(arg1, arg2)
+}
+
+func _swig_wrap_vecdouble_set(SwigcptrVecdouble, int, *float64)
+
+func (arg1 SwigcptrVecdouble) Set(arg2 int, arg3 *float64) {
+ _swig_wrap_vecdouble_set(arg1, arg2, arg3)
+}
+
+func _swig_wrap_vecdouble_getitem(SwigcptrVecdouble, int) float64
+
+func (arg1 SwigcptrVecdouble) Getitem(arg2 int) float64 {
+ return _swig_wrap_vecdouble_getitem(arg1, arg2)
+}
+
+func _swig_wrap_vecdouble_setitem(SwigcptrVecdouble, int, float64)
+
+func (arg1 SwigcptrVecdouble) Setitem(arg2 int, arg3 float64) {
+ _swig_wrap_vecdouble_setitem(arg1, arg2, arg3)
+}
+
+func _swig_wrap_delete_vecdouble(uintptr)
+
+func DeleteVecdouble(arg1 Vecdouble) {
+ _swig_wrap_delete_vecdouble(arg1.Swigcptr())
+}
+
+type Vecdouble interface {
+ Swigcptr() uintptr
+ SwigIsVecdouble()
+ Get(arg2 int) *float64
+ Set(arg2 int, arg3 *float64)
+ Getitem(arg2 int) float64
+ Setitem(arg2 int, arg3 float64)
+}
+
+
+type SwigcptrVoid uintptr
+type Void interface {
+ Swigcptr() uintptr;
+}
+func (p SwigcptrVoid) Swigcptr() uintptr {
+ return uintptr(p)
+}
+
diff --git a/Examples/go/template/example.h b/Examples/go/template/example.h
new file mode 100644
index 000000000..7401df650
--- /dev/null
+++ b/Examples/go/template/example.h
@@ -0,0 +1,32 @@
+/* File : example.h */
+
+// Some template definitions
+
+template T max(T a, T b) { return a>b ? a : b; }
+
+template class vector {
+ T *v;
+ int sz;
+ public:
+ vector(int _sz) {
+ v = new T[_sz];
+ sz = _sz;
+ }
+ T &get(int index) {
+ return v[index];
+ }
+ void set(int index, T &val) {
+ v[index] = val;
+ }
+#ifdef SWIG
+ %extend {
+ T getitem(int index) {
+ return $self->get(index);
+ }
+ void setitem(int index, T val) {
+ $self->set(index,val);
+ }
+ }
+#endif
+};
+
diff --git a/Examples/go/template/example.i b/Examples/go/template/example.i
new file mode 100644
index 000000000..8f94c4da1
--- /dev/null
+++ b/Examples/go/template/example.i
@@ -0,0 +1,17 @@
+/* File : example.i */
+%module example
+
+%{
+#include "example.h"
+%}
+
+/* Let's just grab the original header file here */
+%include "example.h"
+
+/* Now instantiate some specific template declarations */
+
+%template(maxint) max;
+%template(maxdouble) max;
+%template(vecint) vector;
+%template(vecdouble) vector;
+
diff --git a/Examples/go/template/index.html b/Examples/go/template/index.html
new file mode 100644
index 000000000..a14e3b29a
--- /dev/null
+++ b/Examples/go/template/index.html
@@ -0,0 +1,113 @@
+
+
+SWIG:Examples:go:template
+
+
+
+
+
+SWIG/Examples/go/template/
+
+
+
C++ template support
+
+
+This example illustrates how C++ templates can be used from Go using
+SWIG.
+
+
The C++ Code
+
+Lets take a templated function and a templated class as follows:
+
+
+
+/* File : example.h */
+
+// Some template definitions
+
+template T max(T a, T b) { return a>b ? a : b; }
+
+template class vector {
+ T *v;
+ int sz;
+ public:
+ vector(int _sz) {
+ v = new T[_sz];
+ sz = _sz;
+ }
+ T &get(int index) {
+ return v[index];
+ }
+ void set(int index, T &val) {
+ v[index] = val;
+ }
+#ifdef SWIG
+ %addmethods {
+ T getitem(int index) {
+ return self->get(index);
+ }
+ void setitem(int index, T val) {
+ self->set(index,val);
+ }
+ }
+#endif
+};
+
+
+The %addmethods is used for a neater interface from Go as the
+functions get and set use C++ references to
+primitive types. These are tricky to use from Go as they end up as
+pointers, which only work when the C++ and Go types correspond
+precisely.
+
+
The SWIG interface
+
+A simple SWIG interface for this can be built by simply grabbing the
+header file like this:
+
+
+
+/* File : example.i */
+%module example
+
+%{
+#include "example.h"
+%}
+
+/* Let's just grab the original header file here */
+%include "example.h"
+
+/* Now instantiate some specific template declarations */
+
+%template(maxint) max;
+%template(maxdouble) max;
+%template(vecint) vector;
+%template(vecdouble) vector;
+
+
+
+Note that SWIG parses the templated function max and
+templated class vector and so knows about them. However to
+generate code for use from Go, SWIG has to be told which class/type to
+use as the template parameter. The SWIG directive %template is used
+for this.
+
+
A sample Go program
+
+Click here to see a Go program that calls the
+C++ functions from Go.
+
+
Notes
Use templated classes just like you would any other
+SWIG generated Go class. Use the classnames specified by the %template
+directive.
+
+
+
+vecdouble dv = new vecdouble(1000);
+dv.setitem(i, 12.34));
+
+
+
+
+
+
diff --git a/Examples/go/template/runme.go b/Examples/go/template/runme.go
new file mode 100644
index 000000000..8b3d4000e
--- /dev/null
+++ b/Examples/go/template/runme.go
@@ -0,0 +1,43 @@
+// This example illustrates how C++ templates can be used from Go.
+
+package main
+
+import (
+ "fmt"
+ . "./example"
+)
+
+func main() {
+
+ // Call some templated functions
+ fmt.Println(Maxint(3, 7))
+ fmt.Println(Maxdouble(3.14, 2.18))
+
+ // Create some class
+ iv := NewVecint(100)
+ dv := NewVecdouble(1000)
+
+ for i := 0; i < 100; i++ {
+ iv.Setitem(i, 2*i)
+ }
+
+ for i := 0; i < 1000; i++ {
+ dv.Setitem(i, 1.0/float64(i+1))
+ }
+
+ {
+ sum := 0
+ for i := 0; i < 100; i++ {
+ sum = sum + iv.Getitem(i)
+ }
+ fmt.Println(sum)
+ }
+
+ {
+ sum := float64(0.0)
+ for i := 0; i < 1000; i++ {
+ sum = sum + dv.Getitem(i)
+ }
+ fmt.Println(sum)
+ }
+}
diff --git a/Examples/go/variables/Makefile b/Examples/go/variables/Makefile
new file mode 100644
index 000000000..b0aa9c970
--- /dev/null
+++ b/Examples/go/variables/Makefile
@@ -0,0 +1,18 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+SRCS = example.c
+TARGET = example
+INTERFACE = example.i
+SWIGOPT =
+
+all:: go
+
+go::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile go_clean
+
+check: all
+ $(MAKE) -f $(TOP)/Makefile TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' go_run
diff --git a/Examples/go/variables/example.c b/Examples/go/variables/example.c
new file mode 100644
index 000000000..aa4ffe9b3
--- /dev/null
+++ b/Examples/go/variables/example.c
@@ -0,0 +1,91 @@
+/* File : example.c */
+
+/* I'm a file containing some C global variables */
+
+/* Deal with Microsoft's attempt at deprecating C standard runtime functions */
+#if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER)
+# define _CRT_SECURE_NO_DEPRECATE
+#endif
+
+#include
+#include
+#include "example.h"
+
+int ivar = 0;
+short svar = 0;
+long lvar = 0;
+unsigned int uivar = 0;
+unsigned short usvar = 0;
+unsigned long ulvar = 0;
+signed char scvar = 0;
+unsigned char ucvar = 0;
+char cvar = 0;
+float fvar = 0;
+double dvar = 0;
+char *strvar = 0;
+const char cstrvar[] = "Goodbye";
+int *iptrvar = 0;
+char name[256] = "Dave";
+char path[256] = "/home/beazley";
+
+
+/* Global variables involving a structure */
+Point *ptptr = 0;
+Point pt = { 10, 20 };
+
+/* A variable that we will make read-only in the interface */
+int status = 1;
+
+/* A debugging function to print out their values */
+
+void print_vars() {
+ printf("ivar = %d\n", ivar);
+ printf("svar = %d\n", svar);
+ printf("lvar = %ld\n", lvar);
+ printf("uivar = %u\n", uivar);
+ printf("usvar = %u\n", usvar);
+ printf("ulvar = %lu\n", ulvar);
+ printf("scvar = %d\n", scvar);
+ printf("ucvar = %u\n", ucvar);
+ printf("fvar = %g\n", fvar);
+ printf("dvar = %g\n", dvar);
+ printf("cvar = %c\n", cvar);
+ printf("strvar = %s\n", strvar ? strvar : "(null)");
+ printf("cstrvar = %s\n", cstrvar ? cstrvar : "(null)");
+ printf("iptrvar = %p\n", iptrvar);
+ printf("name = %s\n", name);
+ printf("ptptr = %p (%d, %d)\n", ptptr, ptptr ? ptptr->x : 0, ptptr ? ptptr->y : 0);
+ printf("pt = (%d, %d)\n", pt.x, pt.y);
+ printf("status = %d\n", status);
+}
+
+/* A function to create an integer (to test iptrvar) */
+
+int *new_int(int value) {
+ int *ip = (int *) malloc(sizeof(int));
+ *ip = value;
+ return ip;
+}
+
+/* A function to create a point */
+
+Point *new_Point(int x, int y) {
+ Point *p = (Point *) malloc(sizeof(Point));
+ p->x = x;
+ p->y = y;
+ return p;
+}
+
+char * Point_print(Point *p) {
+ static char buffer[256];
+ if (p) {
+ sprintf(buffer,"(%d,%d)", p->x,p->y);
+ } else {
+ sprintf(buffer,"null");
+ }
+ return buffer;
+}
+
+void pt_print() {
+ printf("(%d, %d)\n", pt.x, pt.y);
+}
diff --git a/Examples/go/variables/example.go b/Examples/go/variables/example.go
new file mode 100644
index 000000000..f4f299b73
--- /dev/null
+++ b/Examples/go/variables/example.go
@@ -0,0 +1,198 @@
+/* ----------------------------------------------------------------------------
+ * 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 _swig_wrap_ivar_set(int)
+
+func SetIvar(arg1 int) {
+ _swig_wrap_ivar_set(arg1)
+}
+
+func GetIvar() int
+func _swig_wrap_svar_set(int16)
+
+func SetSvar(arg1 int16) {
+ _swig_wrap_svar_set(arg1)
+}
+
+func GetSvar() int16
+func _swig_wrap_lvar_set(int32)
+
+func SetLvar(arg1 int32) {
+ _swig_wrap_lvar_set(arg1)
+}
+
+func GetLvar() int32
+func _swig_wrap_uivar_set(uint)
+
+func SetUivar(arg1 uint) {
+ _swig_wrap_uivar_set(arg1)
+}
+
+func GetUivar() uint
+func _swig_wrap_usvar_set(uint16)
+
+func SetUsvar(arg1 uint16) {
+ _swig_wrap_usvar_set(arg1)
+}
+
+func GetUsvar() uint16
+func _swig_wrap_ulvar_set(uint32)
+
+func SetUlvar(arg1 uint32) {
+ _swig_wrap_ulvar_set(arg1)
+}
+
+func GetUlvar() uint32
+func _swig_wrap_scvar_set(int8)
+
+func SetScvar(arg1 int8) {
+ _swig_wrap_scvar_set(arg1)
+}
+
+func GetScvar() int8
+func _swig_wrap_ucvar_set(byte)
+
+func SetUcvar(arg1 byte) {
+ _swig_wrap_ucvar_set(arg1)
+}
+
+func GetUcvar() byte
+func _swig_wrap_cvar_set(byte)
+
+func SetCvar(arg1 byte) {
+ _swig_wrap_cvar_set(arg1)
+}
+
+func GetCvar() byte
+func _swig_wrap_fvar_set(float32)
+
+func SetFvar(arg1 float32) {
+ _swig_wrap_fvar_set(arg1)
+}
+
+func GetFvar() float32
+func _swig_wrap_dvar_set(float64)
+
+func SetDvar(arg1 float64) {
+ _swig_wrap_dvar_set(arg1)
+}
+
+func GetDvar() float64
+func _swig_wrap_strvar_set(string)
+
+func SetStrvar(arg1 string) {
+ _swig_wrap_strvar_set(arg1)
+}
+
+func GetStrvar() string
+func GetCstrvar() string
+func _swig_wrap_iptrvar_set(*int)
+
+func SetIptrvar(arg1 *int) {
+ _swig_wrap_iptrvar_set(arg1)
+}
+
+func GetIptrvar() *int
+func _swig_wrap_name_set(string)
+
+func SetName(arg1 string) {
+ _swig_wrap_name_set(arg1)
+}
+
+func GetName() string
+func _swig_wrap_ptptr_set(uintptr)
+
+func SetPtptr(arg1 Point) {
+ _swig_wrap_ptptr_set(arg1.Swigcptr())
+}
+
+func _swig_wrap_ptptr_get() SwigcptrPoint
+
+func GetPtptr() Point {
+ return _swig_wrap_ptptr_get()
+}
+
+func _swig_wrap_pt_set(uintptr)
+
+func SetPt(arg1 Point) {
+ _swig_wrap_pt_set(arg1.Swigcptr())
+}
+
+func _swig_wrap_pt_get() SwigcptrPoint
+
+func GetPt() Point {
+ return _swig_wrap_pt_get()
+}
+
+func GetStatus() int
+func GetPath() string
+func _swig_wrap_print_vars()
+
+func Print_vars() {
+ _swig_wrap_print_vars()
+}
+
+func New_int(int) *int
+func _swig_wrap_new_Point(int, int) SwigcptrPoint
+
+func New_Point(arg1 int, arg2 int) Point {
+ return _swig_wrap_new_Point(arg1, arg2)
+}
+
+func _swig_wrap_Point_print(uintptr) string
+
+func Point_print(arg1 Point) string {
+ return _swig_wrap_Point_print(arg1.Swigcptr())
+}
+
+func _swig_wrap_pt_print()
+
+func Pt_print() {
+ _swig_wrap_pt_print()
+}
+
+
+type SwigcptrPoint uintptr
+type Point interface {
+ Swigcptr() uintptr;
+}
+func (p SwigcptrPoint) Swigcptr() uintptr {
+ return uintptr(p)
+}
+
+type SwigcptrVoid uintptr
+type Void interface {
+ Swigcptr() uintptr;
+}
+func (p SwigcptrVoid) Swigcptr() uintptr {
+ return uintptr(p)
+}
+
diff --git a/Examples/go/variables/example.h b/Examples/go/variables/example.h
new file mode 100644
index 000000000..0f7e89594
--- /dev/null
+++ b/Examples/go/variables/example.h
@@ -0,0 +1,6 @@
+/* File: example.h */
+
+typedef struct {
+ int x,y;
+} Point;
+
diff --git a/Examples/go/variables/example.i b/Examples/go/variables/example.i
new file mode 100644
index 000000000..591b871ed
--- /dev/null
+++ b/Examples/go/variables/example.i
@@ -0,0 +1,49 @@
+/* File : example.i */
+%module example
+%{
+#include "example.h"
+%}
+
+/* Some global variable declarations */
+%inline %{
+extern int ivar;
+extern short svar;
+extern long lvar;
+extern unsigned int uivar;
+extern unsigned short usvar;
+extern unsigned long ulvar;
+extern signed char scvar;
+extern unsigned char ucvar;
+extern char cvar;
+extern float fvar;
+extern double dvar;
+extern char *strvar;
+extern const char cstrvar[];
+extern int *iptrvar;
+extern char name[256];
+
+extern Point *ptptr;
+extern Point pt;
+%}
+
+
+/* Some read-only variables */
+
+%immutable;
+
+%inline %{
+extern int status;
+extern char path[256];
+%}
+
+%mutable;
+
+/* Some helper functions to make it easier to test */
+%inline %{
+extern void print_vars();
+extern int *new_int(int value);
+extern Point *new_Point(int x, int y);
+extern char *Point_print(Point *p);
+extern void pt_print();
+%}
+
diff --git a/Examples/go/variables/index.html b/Examples/go/variables/index.html
new file mode 100644
index 000000000..5a11194df
--- /dev/null
+++ b/Examples/go/variables/index.html
@@ -0,0 +1,87 @@
+
+
+SWIG:Examples:go:variables
+
+
+
+
+SWIG/Examples/go/variables/
+
+
+
Wrapping C Global Variables
+
+
+When a C global variable appears in an interface file, SWIG provides
+getter and setter functions for the variable. The getter function is
+named Get followed by the capitalized name of the variable.
+The setter variable starts with Set instead. The getter
+function takes no parameters and returns the value of the variable.
+The setter function takes a single parameter with the same type as the
+variable, and returns nothing.
+
+
Click here to see a SWIG interface with
+some variable declarations in it.
+
+
Manipulating Variables from Go
+
+For example, if the package is called example, the global
+variable
+
+
+
+double foo;
+
+
+
+will be accessed from Go as
+
+
+example.GetFoo();
+example.SetFoo(12.3);
+
+
+
+Click here to see the example program that
+updates and prints out the values of the variables using this
+technique.
+
+
Key points
+
+
+
The name of the variable is capitalized.
+
When a global variable has the type "char *", SWIG
+manages it as a character string.
+
signed char and unsigned char are handled as
+small 8-bit integers.
+
String array variables such as 'char name[256]' are
+managed as Go strings, but when setting the value, the result is
+truncated to the maximum length of the array. Furthermore, the string
+is assumed to be null-terminated.
+
When structures and classes are used as global variables, they are
+mapped into pointers. Getting the "value" returns a pointer to the
+global variable. Setting the value of a structure results in a memory
+copy from a pointer to the global.
+
+
+
Creating read-only variables
+
+The %immutable and %mutable directives can be used
+to specify a collection of read-only variables. A read only variable
+will have a getter function but no setter function. For example:
+
+