PageRenderTime 41ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Doc/Manual/Go.html

#
HTML | 635 lines | 522 code | 111 blank | 2 comment | 0 complexity | e9af6e2497810b44e9104ae8fc4ac75d MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>SWIG and Go</title>
  5. <link rel="stylesheet" type="text/css" href="style.css">
  6. </head>
  7. <body bgcolor="#FFFFFF">
  8. <H1><a name="Go"></a>22 SWIG and Go</H1>
  9. <!-- INDEX -->
  10. <div class="sectiontoc">
  11. <ul>
  12. <li><a href="#Go_overview">Overview</a>
  13. <li><a href="#Go_running_swig">Running SWIG with Go</a>
  14. <ul>
  15. <li><a href="#Go_commandline">Additional Commandline Options</a>
  16. <li><a href="#Go_outputs">Go Output Files</a>
  17. </ul>
  18. <li><a href="#Go_basic_tour">A tour of basic C/C++ wrapping</a>
  19. <ul>
  20. <li><a href="#Go_package">Go Package Name</a>
  21. <li><a href="#Go_names">Go Names</a>
  22. <li><a href="#Go_constants">Go Constants</a>
  23. <li><a href="#Go_enumerations">Go Enumerations</a>
  24. <li><a href="#Go_classes">Go Classes</a>
  25. <ul>
  26. <li><a href="#Go_class_inheritance">Go Class Inheritance</a>
  27. </ul>
  28. <li><a href="#Go_templates">Go Templates</a>
  29. <li><a href="#Go_director_classes">Go Director Classes</a>
  30. <li><a href="#Go_primitive_type_mappings">Default Go primitive type mappings</a>
  31. <li><a href="#Go_output_arguments">Output arguments</a>
  32. <li><a href="#Go_adding_additional_code">Adding additional go code</a>
  33. </ul>
  34. </ul>
  35. </div>
  36. <!-- INDEX -->
  37. <p>
  38. This chapter describes SWIG's support of Go. For more information on
  39. the Go programming language
  40. see <a href="http://golang.org/">golang.org</a>.
  41. </p>
  42. <H2><a name="Go_overview"></a>22.1 Overview</H2>
  43. <p>
  44. Go is a compiled language, not a scripting language. However, it does
  45. not support direct calling of functions written in C/C++. The cgo
  46. program may be used to generate wrappers to call C code from Go, but
  47. there is no convenient way to call C++ code. SWIG fills this gap.
  48. </p>
  49. <p>
  50. There are (at least) two different Go compilers. One is the gc
  51. compiler, normally invoked under the names 6g, 8g, or 5g. The other
  52. is the gccgo compiler, which is a frontend to the gcc compiler suite.
  53. The interface to C/C++ code is completely different for the two Go
  54. compilers. SWIG supports both, selected by a command line option.
  55. </p>
  56. <p>
  57. Because Go is a type-safe compiled language, SWIG's runtime type
  58. checking and runtime library are not used with Go. This should be
  59. borne in mind when reading the rest of the SWIG documentation.
  60. </p>
  61. <H2><a name="Go_running_swig"></a>22.2 Running SWIG with Go</H2>
  62. <p>
  63. To generate Go code, use the <tt>-go</tt> option with SWIG. By
  64. default SWIG will generate code for the gc compilers. To generate
  65. code for gccgo, you should also use the <tt>-gccgo</tt> option.
  66. </p>
  67. <H3><a name="Go_commandline"></a>22.2.1 Additional Commandline Options</H3>
  68. <p>
  69. These are the command line options for SWIG's GO module. They can
  70. also be seen by using:
  71. </p>
  72. <div class="code"><pre>
  73. swig -go -help
  74. </pre></div>
  75. <table summary="Go specific options">
  76. <tr>
  77. <th>Go specific options</th>
  78. </tr>
  79. <tr>
  80. <td>-gccgo</td>
  81. <td>Generate code for gccgo. The default is to generate code for
  82. 6g/8g/5g.</td>
  83. </tr>
  84. <tr>
  85. <td>-gccgo-46</td>
  86. <td>Generate code for gccgo 4.6. The default is set by the configure
  87. script.</td> This generates code that does not use some facilities
  88. that are only available in gccgo 4.7 and later.
  89. </tr>
  90. <tr>
  91. <td>-no-gccgo-46</td>
  92. <td>Turn off <code>-gccgo-46</code>, whether set by default or earlier
  93. on the command line.
  94. </tr>
  95. <tr>
  96. <td>-package &lt;name&gt;</td>
  97. <td>Set the name of the Go package to &lt;name&gt;. The default
  98. package name is the SWIG module name.</td>
  99. </tr>
  100. <tr>
  101. <td>-soname %lt;name%gt;</td>
  102. <td>Set the runtime name of the shared library that the dynamic linker
  103. should include at runtime. The default is the package name with
  104. ".so" appended. This is only used when generating code for
  105. 6g/8g/5g; when using gccgo, the equivalent name will be taken from
  106. the <code>-soname</code> option passed to the linker.</td>
  107. </tr>
  108. <tr>
  109. <td>-go-prefix &lt;prefix&gt;</td>
  110. <td>When generating code for gccgo, set the prefix to use. This
  111. corresponds to the <tt>-fgo-prefix</tt> option to gccgo.</td>
  112. </tr>
  113. <tr>
  114. <td>-long-type-size &lt;s&gt;</td>
  115. <td>Set the size for the C/C++ type <tt>long</tt>. This controls
  116. whether <tt>long</tt> is converted to the Go type <tt>int32</tt>
  117. or <tt>int64</tt>. The &lt;s&gt; argument should be 32 or 64.</td>
  118. </tr>
  119. </table>
  120. <H3><a name="Go_outputs"></a>22.2.2 Go Output Files</H3>
  121. <p> When generating Go code, SWIG will generate the following
  122. files:</p>
  123. <ul>
  124. <li>
  125. MODULE.go will contain the Go functions that your Go code will call.
  126. These functions will be wrappers for the C++ functions defined by your
  127. module. This file should, of course, be compiled with the Go
  128. compiler.
  129. <li>
  130. MODULE_wrap.c or MODULE_wrap.cxx will contain C/C++ functions will be
  131. invoked by the Go wrapper code. This file should be compiled with the
  132. usual C or C++ compiler and linked into a shared library.
  133. <li>
  134. MODULE_wrap.h will be generated if you use the directors feature. It
  135. provides a definition of the generated C++ director classes. It is
  136. generally not necessary to use this file, but in some special cases it
  137. may be helpful to include it in your code, compiled with the usual C
  138. or C++ compiler.
  139. <li>
  140. If using the gc compiler, MODULE_gc.c will contain C code which should
  141. be compiled with the C compiler distributed as part of the gc compiler: 6c, 8c,
  142. or 5c. It should then be combined with the compiled MODULE.go using
  143. gopack. This file will not be generated when using gccgo.
  144. </ul>
  145. <p>
  146. A typical command sequence would look like this:
  147. </p>
  148. <div class="code"><pre>
  149. % swig -go example.i
  150. % gcc -c -fpic example.c
  151. % gcc -c -fpic example_wrap.c
  152. % gcc -shared example.o example_wrap.o -o example.so
  153. % 6g example.go
  154. % 6c example_gc.c
  155. % gopack grc example.a example.6 example_gc.6
  156. % 6g main.go # your code, not generated by SWIG
  157. % 6l main.6
  158. </pre></div>
  159. <H2><a name="Go_basic_tour"></a>22.3 A tour of basic C/C++ wrapping</H2>
  160. <p>
  161. By default, SWIG attempts to build a natural Go interface to your
  162. C/C++ code. However, the languages are somewhat different, so some
  163. modifications have to occur. This section briefly covers the
  164. essential aspects of this wrapping.
  165. </p>
  166. <H3><a name="Go_package"></a>22.3.1 Go Package Name</H3>
  167. <p>
  168. All Go source code lives in a package. The name of this package will
  169. default to the name of the module from SWIG's <tt>%module</tt>
  170. directive. You may override this by using SWIG's <tt>-package</tt>
  171. command line option.
  172. </p>
  173. <H3><a name="Go_names"></a>22.3.2 Go Names</H3>
  174. <p>
  175. In Go, a function is only visible outside the current package if the
  176. first letter of the name is uppercase. This is quite different from
  177. C/C++. Because of this, C/C++ names are modified when generating the
  178. Go interface: the first letter is forced to be uppercase if it is not
  179. already. This affects the names of functions, methods, variables,
  180. constants, enums, and classes.
  181. </p>
  182. <p>
  183. C/C++ variables are wrapped with setter and getter functions in Go.
  184. First the first letter of the variable name will be forced to
  185. uppercase, and then <tt>Get</tt> or <tt>Set</tt> will be prepended.
  186. For example, if the C/C++ variable is called <tt>var</tt>, then SWIG
  187. will define the functions <tt>GetVar</tt> and <tt>SetVar</tt>. If a
  188. variable is declared as <tt>const</tt>, or if
  189. SWIG's <a href="SWIG.html#SWIG_readonly_variables">
  190. <tt>%immutable</tt> directive</a> is used for the variable, then only
  191. the getter will be defined.
  192. </p>
  193. <p>
  194. C++ classes will be discussed further below. Here we'll note that the
  195. first letter of the class name will be forced to uppercase to give the
  196. name of a type in Go. A constructor will be named <tt>New</tt>
  197. followed by that name, and the destructor will be
  198. named <tt>Delete</tt> followed by that name.
  199. </p>
  200. <H3><a name="Go_constants"></a>22.3.3 Go Constants</H3>
  201. <p>
  202. C/C++ constants created via <tt>#define</tt> or the <tt>%constant</tt>
  203. directive become Go constants, declared with a <tt>const</tt>
  204. declaration.
  205. <H3><a name="Go_enumerations"></a>22.3.4 Go Enumerations</H3>
  206. <p>
  207. C/C++ enumeration types will cause SWIG to define an integer type with
  208. the name of the enumeration (with first letter forced to uppercase as
  209. usual). The values of the enumeration will become variables in Go;
  210. code should avoid modifying those variables.
  211. </p>
  212. <H3><a name="Go_classes"></a>22.3.5 Go Classes</H3>
  213. <p>
  214. Go has interfaces, methods and inheritance, but it does not have
  215. classes in the same sense as C++. This sections describes how SWIG
  216. represents C++ classes represented in Go.
  217. </p>
  218. <p>
  219. For a C++ class <tt>ClassName</tt>, SWIG will define two types in Go:
  220. an underlying type, which will just hold a pointer to the C++ type,
  221. and an interface type. The interface type will be
  222. named <tt>ClassName</tt>. SWIG will define a
  223. function <tt>NewClassName</tt> which will take any constructor
  224. arguments and return a value of the interface
  225. type <tt>ClassName</tt>. SWIG will also define a
  226. destructor <tt>DeleteClassName</tt>.
  227. </p>
  228. <p>
  229. SWIG will represent any methods of the C++ class as methods on the
  230. underlying type, and also as methods of the interface type. Thus C++
  231. methods may be invoked directly using the
  232. usual <tt>val.MethodName</tt> syntax. Public members of the C++ class
  233. will be given getter and setter functions defined as methods of the
  234. class.
  235. </p>
  236. <p>
  237. SWIG will represent static methods of C++ classes as ordinary Go
  238. functions. SWIG will use names like <tt>ClassNameMethodName</tt>.
  239. SWIG will give static members getter and setter functions with names
  240. like <tt>GetClassName_VarName</tt>.
  241. </p>
  242. <p>
  243. Given a value of the interface type, Go code can retrieve the pointer
  244. to the C++ type by calling the <tt>Swigcptr</tt> method. This will
  245. return a value of type <tt>SwigcptrClassName</tt>, which is just a
  246. name for <tt>uintptr</tt>. A Go type conversion can be used to
  247. convert this value to a different C++ type, but note that this
  248. conversion will not be type checked and is essentially equivalent
  249. to <tt>reinterpret_cast</tt>. This should only be used for very
  250. special cases, such as where C++ would use a <tt>dynamic_cast</tt>.
  251. </p>
  252. <p>Note that C++ pointers to compound objects are represented in go as objects
  253. themselves, not as go pointers. So, for example, if you wrap the following
  254. function:</p>
  255. <div class="code">
  256. <pre>
  257. class MyClass {
  258. int MyMethod();
  259. static MyClass *MyFactoryFunction();
  260. };
  261. </pre>
  262. </div>
  263. <p>You will get go code that looks like this:</p>
  264. <div class="code">
  265. <pre>
  266. type MyClass interface {
  267. Swigcptr() uintptr
  268. SwigIsMyClass()
  269. MyMethod() int
  270. }
  271. MyClassMyFactoryFunction() MyClass {
  272. // swig magic here
  273. }
  274. </pre>
  275. </div>
  276. <p>Note that the factory function does not return a go pointer; it actually
  277. returns a go interface. If the returned pointer can be null, you can check
  278. for this by calling the Swigcptr() method.
  279. </p>
  280. <H4><a name="Go_class_inheritance"></a>22.3.5.1 Go Class Inheritance</H4>
  281. <p>
  282. C++ class inheritance is automatically represented in Go due to its
  283. use of interfaces. The interface for a child class will be a superset
  284. of the interface of its parent class. Thus a value of the child class
  285. type in Go may be passed to a function which expects the parent class.
  286. Doing the reverse will require an explicit type assertion, which will
  287. be checked dynamically.
  288. </p>
  289. <H3><a name="Go_templates"></a>22.3.6 Go Templates</H3>
  290. <p>
  291. In order to use C++ templates in Go, you must tell SWIG to create
  292. wrappers for a particular template instantation. To do this, use
  293. the <tt>%template</tt> directive.
  294. <H3><a name="Go_director_classes"></a>22.3.7 Go Director Classes</H3>
  295. <p>
  296. SWIG's director feature permits a Go type to act as the subclass of a
  297. C++ class with virtual methods. This is complicated by the fact that
  298. C++ and Go define inheritance differently. In Go, structs can inherit
  299. methods via anonymous field embedding. However, when a method is
  300. called for an embedded struct, if that method calls any other methods,
  301. they are called for the embedded struct, not for the original type.
  302. Therefore, SWIG must use Go interfaces to represent C++ inheritance.
  303. </p>
  304. <p>
  305. In order to use the director feature in Go, you must define a type in
  306. your Go code. You must then add methods for the type. Define a
  307. method in Go for each C++ virtual function that you want to override.
  308. You must then create a value of your new type, and pass a pointer to
  309. it to the function <tt>NewDirectorClassName</tt>,
  310. where <tt>ClassName</tt> is the name of the C++ class. That will
  311. return a value of type <tt>ClassName</tt>.
  312. </p>
  313. <p>
  314. For example:
  315. </p>
  316. <div class="code">
  317. <pre>
  318. type GoClass struct { }
  319. func (p *GoClass) VirtualFunction() { }
  320. func MakeClass() ClassName {
  321. return NewDirectorClassName(&amp;GoClass{})
  322. }
  323. </pre>
  324. </div>
  325. <p>
  326. Any call in C++ code to the virtual function will wind up calling the
  327. method defined in Go. The Go code may of course call other methods on
  328. itself, and those methods may be defined either in Go or in C++.
  329. </p>
  330. <H3><a name="Go_primitive_type_mappings"></a>22.3.8 Default Go primitive type mappings</H3>
  331. <p>
  332. The following table lists the default type mapping from C/C++ to Go.
  333. This table will tell you which Go type to expect for a function which
  334. uses a given C/C++ type.
  335. </p>
  336. <table BORDER summary="Go primitive type mappings">
  337. <tr>
  338. <td><b>C/C++ type</b></td>
  339. <td><b>Go type</b></td>
  340. </tr>
  341. <tr>
  342. <td>bool</td>
  343. <td>bool</td>
  344. </tr>
  345. <tr>
  346. <td>char</td>
  347. <td>byte</td>
  348. </tr>
  349. <tr>
  350. <td>signed char</td>
  351. <td>int8</td>
  352. </tr>
  353. <tr>
  354. <td>unsigned char</td>
  355. <td>byte</td>
  356. </tr>
  357. <tr>
  358. <td>short</td>
  359. <td>int16</td>
  360. </tr>
  361. <tr>
  362. <td>unsigned short</td>
  363. <td>uint16</td>
  364. </tr>
  365. <tr>
  366. <td>int</td>
  367. <td>int</td>
  368. </tr>
  369. <tr>
  370. <td>unsigned int</td>
  371. <td>uint</td>
  372. </tr>
  373. <tr>
  374. <td>long</td>
  375. <td>int32 or int64, depending on <tt>-long-type-size</tt></td>
  376. </tr>
  377. <tr>
  378. <td>unsigned long</td>
  379. <td>uint32 or uint64, depending on <tt>-long-type-size</tt></td>
  380. </tr>
  381. <tr>
  382. <td>long long</td>
  383. <td>int64</td>
  384. </tr>
  385. <tr>
  386. <td>unsigned long long</td>
  387. <td>uint64</td>
  388. </tr>
  389. <tr>
  390. <td>float</td>
  391. <td>float32</td>
  392. </tr>
  393. <tr>
  394. <td>double</td>
  395. <td>float64</td>
  396. </tr>
  397. <tr>
  398. <td>char *<br>char []</td>
  399. <td>string</td>
  400. </tr>
  401. </table>
  402. <p>
  403. Note that SWIG wraps the C <tt>char</tt> type as a character. Pointers
  404. and arrays of this type are wrapped as strings. The <tt>signed
  405. char</tt> type can be used if you want to treat <tt>char</tt> as a
  406. signed number rather than a character. Also note that all const
  407. references to primitive types are treated as if they are passed by
  408. value.
  409. </p>
  410. <p>
  411. These type mappings are defined by the "gotype" typemap. You may change
  412. that typemap, or add new values, to control how C/C++ types are mapped
  413. into Go types.
  414. </p>
  415. <H3><a name="Go_output_arguments"></a>22.3.9 Output arguments</H3>
  416. <p>Because of limitations in the way output arguments are processed in swig,
  417. a function with output arguments will not have multiple return values.
  418. Instead, you must pass a pointer into the C++ function to tell it where to
  419. store the ouput value. In go, you supply a slice in the place of the output
  420. argument.</p>
  421. <p>For example, suppose you were trying to wrap the modf() function in the
  422. C math library which splits x into integral and fractional parts (and
  423. returns the integer part in one of its parameters):</p>
  424. <div class="code">
  425. <pre>
  426. double modf(double x, double *ip);
  427. </pre>
  428. </div>
  429. <p>You could wrap it with SWIG as follows:</p>
  430. <div class="code">
  431. <pre>
  432. %include &lt;typemaps.i&gt;
  433. double modf(double x, double *OUTPUT);
  434. </pre>
  435. </div>
  436. <p>or you can use the <code>%apply</code> directive:</p>
  437. <div class="code">
  438. <pre>
  439. %include &lt;typemaps.i&gt;
  440. %apply double *OUTPUT { double *ip };
  441. double modf(double x, double *ip);
  442. </pre>
  443. </div>
  444. <p>In Go you would use it like this:</p>
  445. <div class="code">
  446. <pre>
  447. ptr := []float64{0.0}
  448. fraction := modulename.Modf(5.0, ptr)
  449. </pre>
  450. </div>
  451. <p>Since this is ugly, you may want to wrap the swig-generated API with
  452. some <a href="#Embedded_go_code">additional functions written in go</a> that
  453. hide the ugly details.</p>
  454. <p>There are no <code>char&nbsp;*OUTPUT</code> typemaps. However you can
  455. apply the <code>signed&nbsp;char&nbsp;*</code> typemaps instead:</p>
  456. <div class="code">
  457. <pre>
  458. %include &lt;typemaps.i&gt;
  459. %apply signed char *OUTPUT {char *output};
  460. void f(char *output);
  461. </pre>
  462. </div>
  463. <H3><a name="Go_adding_additional_code"></a>22.3.10 Adding additional go code</H3>
  464. <p>Often the APIs generated by swig are not very natural in go, especially if
  465. there are output arguments. You can
  466. insert additional go wrapping code to add new APIs
  467. with <code>%insert(go_wrapper)</code>, like this:</p>
  468. <div class="code">
  469. <pre>
  470. %include &lt;typemaps.i&gt;
  471. // Change name of what swig generates to Wrapped_modf. This function will
  472. // have the following signature in go:
  473. // func Wrapped_modf(float64, []float64) float64
  474. %rename(wrapped_modf) modf(double x, double *ip);
  475. %apply double *OUTPUT { double *ip };
  476. double modf(double x, double *ip);
  477. %insert(go_wrapper) %{
  478. // The improved go interface to this function, which has two return values,
  479. // in the more natural go idiom:
  480. func Modf(x float64) (fracPart float64, intPart float64) {
  481. ip := []float64{0.0}
  482. fracPart = Wrapped_modf(x, ip)
  483. intPart = ip[0]
  484. return
  485. }
  486. %}
  487. </pre>
  488. </div>
  489. <p>For classes, since swig generates an interface, you can add additional
  490. methods by defining another interface that includes the swig-generated
  491. interface. For example,</p>
  492. <div class="code">
  493. <pre>
  494. %rename(Wrapped_MyClass) MyClass;
  495. %rename(Wrapped_GetAValue) MyClass::GetAValue(int *x);
  496. %apply int *OUTPUT { int *x };
  497. class MyClass {
  498. public:
  499. MyClass();
  500. int AFineMethod(const char *arg); // Swig's wrapping is fine for this one.
  501. bool GetAValue(int *x);
  502. };
  503. %insert(go_wrapper) %{
  504. type MyClass interface {
  505. Wrapped_MyClass
  506. GetAValue() (int, bool)
  507. }
  508. func (arg SwigcptrWrapped_MyClass) GetAValue() (int, bool) {
  509. ip := []int{0}
  510. ok := arg.Wrapped_GetAValue(ip)
  511. return ip[0], ok
  512. }
  513. %}
  514. </pre>
  515. </div>
  516. <p>Of course, if you have to rewrite most of the methods, instead of just a
  517. few, then you might as well define your own struct that includes the
  518. swig-wrapped object, instead of adding methods to the swig-generated object.</p>
  519. <p>This only works if your wrappers do not need to import other go modules.
  520. There is at present no way to insert import statements in the correct place
  521. in swig-generated go. If you need to do that, you must put your go code
  522. in a separate file.</p>
  523. </body>
  524. </html>