# 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.
- enum. Wrapping enumerations.
- class. Wrapping a simple C++ class.
- reference. C++ references.
- pointer. Simple pointer handling.
- funcptr. Pointers to functions.
- template. C++ templates.
- callback. C++ callbacks using directors.
- extend. Polymorphism using directors.
- director. Example how to utilize the director feature.
- director. Example how to use goin and godirectorin.

## Compilation Issues

- 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`. Normally builds are done simply using `go build`. For testing purposes this makefile performs a manual module compilation that is platform specific. When using the `gc` compiler, the steps look approximately like this (GNU/Linux):

  >     % swig -go -cgo interface.i
  >     % mkdir -p gopath/src/interface
  >     % cp interface_wrap.c interface_wrap.h interface.go gopath/src/interface
  >     % GOPATH=`pwd`/gopath
  >     % export GOPATH
  >     % cd gopath/src/interface
  >     % go build
  >     % go tool compile $(SRCDIR)/runme.go
  >     % go tool link -o runme runme.o

- When using the `gccgo` compiler, the steps look like this:

  >     % swig -go -cgo interface.i
  >     % mkdir -p gopath/src/interface
  >     % cp interface_wrap.c interface_wrap.h interface.go gopath/src/interface
  >     % GOPATH=`pwd`/gopath
  >     % export GOPATH
  >     % cd gopath/src/interface
  >     % go build
  >     % gccgo -c $(SRCDIR)/runme.go
  >     % gccgo -o runme runme.o interface.a

  ## Compatibility

  The examples have been extensively tested on the following platforms:
  - GNU/Linux
  All of the examples were last tested with the following configuration (5 August 2015):
  - Ubuntu Trusty
  - gcc-4.8.4
  Your mileage may vary. If you experience a problem, please let us know by contacting us on the mailing lists.