<html lang="en"> <head> <title>Function Names - 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="prev" href="C-Type-Interoperability.html#C-Type-Interoperability" title="C Type Interoperability"> <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="Function-Names"></a> <p> Previous: <a rel="previous" accesskey="p" href="C-Type-Interoperability.html#C-Type-Interoperability">C Type Interoperability</a>, Up: <a rel="up" accesskey="u" href="C-Interoperability.html#C-Interoperability">C Interoperability</a> <hr> </div> <h3 class="section">3.2 Function Names</h3> <p><a name="index-g_t_0040code_007b_005f_005fasm_005f_005f_007d-10"></a>Go code can call C functions directly using a Go extension implemented in <samp><span class="command">gccgo</span></samp>: a function declaration may be followed by <code>__asm__ ("</code><var>name</var><code>")</code>. For example, here is how the C function <code>open</code> can be declared in Go: <pre class="smallexample"> func c_open(name *byte, mode int, perm int) int __asm__ ("open"); </pre> <p>The C function naturally expects a nul terminated string, which in Go is equivalent to a pointer to an array (not a slice!) of <code>byte</code> with a terminating zero byte. So a sample call from Go would look like (after importing the <code>os</code> package): <pre class="smallexample"> var name = [4]byte{'f', 'o', 'o', 0}; i := c_open(&amp;name[0], os.O_RDONLY, 0); </pre> <p>Note that this serves as an example only. To open a file in Go please use Go's <code>os.Open</code> function instead. <p>The name of Go functions accessed from C is subject to change. At present the name of a Go function that does not have a receiver is <code>prefix.package.Functionname</code>. The prefix is set by the <samp><span class="option">-fgo-prefix</span></samp> option used when the package is compiled; if the option is not used, the default is simply <code>go</code>. To call the function from C you must set the name using the <samp><span class="command">gcc</span></samp> extension similar to the <samp><span class="command">gccgo</span></samp> extension. <pre class="smallexample"> extern int go_function(int) __asm__ ("myprefix.mypackage.Function"); </pre> </body></html>