<html lang="en"> <head> <title>C Type Interoperability - The GNU Go Compiler</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="The GNU Go Compiler"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="C-Interoperability.html#C-Interoperability" title="C Interoperability"> <link rel="next" href="Function-Names.html#Function-Names" title="Function Names"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <!-- Copyright (C) 2010 Free Software Foundation, Inc. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, the Front-Cover Texts being (a) (see below), and with the Back-Cover Texts being (b) (see below). A copy of the license is included in the section entitled ``GNU Free Documentation License''. (a) The FSF's Front-Cover Text is: A GNU Manual (b) The FSF's Back-Cover Text is: You have freedom to copy and modify this GNU Manual, like GNU software. Copies published by the Free Software Foundation raise funds for GNU development. --> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="C-Type-Interoperability"></a> <p> Next: <a rel="next" accesskey="n" href="Function-Names.html#Function-Names">Function Names</a>, Up: <a rel="up" accesskey="u" href="C-Interoperability.html#C-Interoperability">C Interoperability</a> <hr> </div> <h3 class="section">3.1 C Type Interoperability</h3> <p>Basic types map directly: an <code>int</code> in Go is an <code>int</code> in C, etc. Go <code>byte</code> is equivalent to C <code>unsigned char</code>. Pointers in Go are pointers in C. A Go <code>struct</code> is the same as C <code>struct</code> with the same field names and types. <p><a name="index-g_t_0040code_007bstring_007d-in-C-8"></a>The Go <code>string</code> type is currently defined as a two-element structure: <pre class="smallexample"> struct __go_string { const unsigned char *__data; int __length; }; </pre> <p>You can't pass arrays between C and Go. However, a pointer to an array in Go is equivalent to a C pointer to the equivalent of the element type. For example, Go <code>*[10]int</code> is equivalent to C <code>int*</code>, assuming that the C pointer does point to 10 elements. <p><a name="index-g_t_0040code_007bslice_007d-in-C-9"></a>A slice in Go is a structure. The current definition is: <pre class="smallexample"> struct __go_slice { void *__values; int __count; int __capacity; }; </pre> <p>The type of a Go function with no receiver is equivalent to a C function whose parameter types are equivalent. When a Go function returns more than one value, the C function returns a struct. For example, these functions have equivalent types: <pre class="smallexample"> func GoFunction(int) (int, float) struct { int i; float f; } CFunction(int) </pre> <p>A pointer to a Go function is equivalent to a pointer to a C function when the functions have equivalent types. <p>Go <code>interface</code>, <code>channel</code>, and <code>map</code> types have no corresponding C type (<code>interface</code> is a two-element struct and <code>channel</code> and <code>map</code> are pointers to structs in C, but the structs are deliberately undocumented). C <code>enum</code> types correspond to some integer type, but precisely which one is difficult to predict in general; use a cast. C <code>union</code> types have no corresponding Go type. C <code>struct</code> types containing bitfields have no corresponding Go type. C++ <code>class</code> types have no corresponding Go type. <p>Memory allocation is completely different between C and Go, as Go uses garbage collection. The exact guidelines in this area are undetermined, but it is likely that it will be permitted to pass a pointer to allocated memory from C to Go. The responsibility of eventually freeing the pointer will remain with C side, and of course if the C side frees the pointer while the Go side still has a copy the program will fail. When passing a pointer from Go to C, the Go function must retain a visible copy of it in some Go variable. Otherwise the Go garbage collector may delete the pointer while the C function is still using it. </body></html>