github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/doc/articles/c_go_cgo.html (about)

     1  <!--{
     2  "Title": "C? Go? Cgo!",
     3  "Template": true
     4  }-->
     5  
     6  <p>
     7  Cgo lets Go packages call C code. Given a Go source file written with some
     8  special features, cgo outputs Go and C files that can be combined into a
     9  single Go package.
    10  </p>
    11  
    12  <p>
    13  To lead with an example, here's a Go package that provides two functions -
    14  <code>Random</code> and <code>Seed</code> - that wrap C's <code>rand</code>
    15  and <code>srand</code> functions.
    16  </p>
    17  
    18  {{code "/doc/progs/cgo1.go" `/package rand/` `/END/`}}
    19  
    20  <p>
    21  Let's look at what's happening here, starting with the import statement.
    22  </p>
    23  
    24  <p>
    25  The <code>rand</code> package imports <code>"C"</code>, but you'll find there's
    26  no such package in the standard Go library. That's because <code>C</code> is a
    27  "pseudo-package", a special name interpreted by cgo as a reference to C's
    28  name space.
    29  </p>
    30  
    31  <p>
    32  The <code>rand</code> package contains four references to the <code>C</code>
    33  package: the calls to <code>C.rand</code> and <code>C.srand</code>, the
    34  conversion <code>C.uint(i)</code>, and the <code>import</code> statement.
    35  </p>
    36  
    37  <p>
    38  The <code>Random</code> function calls the standard C library's <code>random</code>
    39  function and returns the result.  In C, <code>rand</code> returns a value of the
    40  C type <code>int</code>, which cgo represents as the type <code>C.int</code>.
    41  It must be converted to a Go type before it can be used by Go code outside this
    42  package, using an ordinary Go type conversion:
    43  </p>
    44  
    45  {{code "/doc/progs/cgo1.go" `/func Random/` `/STOP/`}}
    46  
    47  <p>
    48  Here's an equivalent function that uses a temporary variable to illustrate
    49  the type conversion more explicitly:
    50  </p>
    51  
    52  {{code "/doc/progs/cgo2.go" `/func Random/` `/STOP/`}}
    53  
    54  <p>
    55  The <code>Seed</code> function does the reverse, in a way. It takes a
    56  regular Go <code>int</code>, converts it to the C <code>unsigned int</code>
    57  type, and passes it to the C function <code>srand</code>.
    58  </p>
    59  
    60  {{code "/doc/progs/cgo1.go" `/func Seed/` `/END/`}}
    61  
    62  <p>
    63  Note that cgo knows the <code>unsigned int</code> type as <code>C.uint</code>;
    64  see the <a href="/cmd/cgo">cgo documentation</a> for a complete list of
    65  these numeric type names.
    66  </p>
    67  
    68  <p>
    69  The one detail of this example we haven't examined yet is the comment
    70  above the <code>import</code> statement.
    71  </p>
    72  
    73  {{code "/doc/progs/cgo1.go" `/\/\*/` `/STOP/`}}
    74  
    75  <p>
    76  Cgo recognizes this comment.  Any lines starting
    77  with <code>#cgo</code>
    78  followed
    79  by a space character are removed; these become directives for cgo.
    80  The remaining lines are used as a header when compiling the C parts of
    81  the package.  In this case those lines are just a
    82  single <code>#include</code>
    83  statement, but they can be almost any C code.  The <code>#cgo</code>
    84  directives are
    85  used to provide flags for the compiler and linker when building the C
    86  parts of the package.
    87  </p>
    88  
    89  <p>
    90  There is a limitation: if your program uses any <code>//export</code>
    91  directives, then the C code in the comment may only include declarations
    92  (<code>extern int f();</code>), not definitions (<code>int f() {
    93  return 1; }</code>).  You can use <code>//export</code> directives to
    94  make Go functions accessible to C code.
    95  </p>
    96  
    97  <p>
    98  The <code>#cgo</code> and <code>//export</code> directives are
    99  documented in
   100  the <a href="/cmd/cgo/">cgo documentation</a>.
   101  </p>
   102  
   103  <p>
   104  <b>Strings and things</b>
   105  </p>
   106  
   107  <p>
   108  Unlike Go, C doesn't have an explicit string type. Strings in C are
   109  represented by a zero-terminated array of chars.
   110  </p>
   111  
   112  <p>
   113  Conversion between Go and C strings is done with the
   114  <code>C.CString</code>, <code>C.GoString</code>, and
   115  <code>C.GoStringN</code> functions. These conversions make a copy of the
   116  string data.
   117  </p>
   118  
   119  <p>
   120  This next example implements a <code>Print</code> function that writes a
   121  string to standard output using C's <code>fputs</code> function from the
   122  <code>stdio</code> library:
   123  </p>
   124  
   125  {{code "/doc/progs/cgo3.go" `/package print/` `/END/`}}
   126  
   127  <p>
   128  Memory allocations made by C code are not known to Go's memory manager.
   129  When you create a C string with <code>C.CString</code> (or any C memory
   130  allocation) you must remember to free the memory when you're done with it
   131  by calling <code>C.free</code>.
   132  </p>
   133  
   134  <p>
   135  The call to <code>C.CString</code> returns a pointer to the start of the
   136  char array, so before the function exits we convert it to an
   137  <a href="/pkg/unsafe/#Pointer"><code>unsafe.Pointer</code></a> and release
   138  the memory allocation with <code>C.free</code>. A common idiom in cgo programs
   139  is to <a href="/doc/articles/defer_panic_recover.html"><code>defer</code></a>
   140  the free immediately after allocating (especially when the code that follows
   141  is more complex than a single function call), as in this rewrite of
   142  <code>Print</code>:
   143  </p>
   144  
   145  {{code "/doc/progs/cgo4.go" `/func Print/` `/END/`}}
   146  
   147  <p>
   148  <b>Building cgo packages</b>
   149  </p>
   150  
   151  <p>
   152  To build cgo packages, just use <a href="/cmd/go/#hdr-Compile_packages_and_dependencies">"
   153  <code>go build</code>"</a> or
   154  <a href="/cmd/go/#hdr-Compile_and_install_packages_and_dependencies">"<code>go install</code>
   155  "</a> as usual. The go tool recognizes the special <code>"C"</code> import and automatically
   156  uses cgo for those files.
   157  </p>
   158  
   159  <p>
   160  <b>More cgo resources</b>
   161  </p>
   162  
   163  <p>
   164  The <a href="/cmd/cgo/">cgo command</a> documentation has more detail about
   165  the C pseudo-package and the build process. The <a href="/misc/cgo/">cgo examples</a>
   166  in the Go tree demonstrate more advanced concepts.
   167  </p>
   168  
   169  <p>
   170  For a simple, idiomatic example of a cgo-based package, see Russ Cox's <a
   171  href="http://code.google.com/p/gosqlite/source/browse/sqlite/sqlite.go">gosqlite</a>.
   172  Also, the <a href="http://code.google.com/p/go-wiki/wiki/Projects">Go Community Wiki</a>
   173  lists many packages, some of which use cgo.
   174  </p>
   175  
   176  <p>
   177  Finally, if you're curious as to how all this works internally, take a look
   178  at the introductory comment of the runtime package's <a href="/src/pkg/runtime/cgocall.c">cgocall.c</a>.
   179  </p>