]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/go/doc/c-type-interoperability.rst
sphinx: add missing trailing newline
[thirdparty/gcc.git] / gcc / go / doc / c-type-interoperability.rst
CommitLineData
c63539ff
ML
1..
2 Copyright 1988-2022 Free Software Foundation, Inc.
3 This is part of the GCC manual.
4 For copying conditions, see the copyright.rst file.
5
6.. _c-type-interoperability:
7
8C Type Interoperability
9***********************
10
11Basic types map directly: an ``int`` in Go is an ``int`` in C,
12etc. Go ``byte`` is equivalent to C ``unsigned char``.
13Pointers in Go are pointers in C. A Go ``struct`` is the same as C
14``struct`` with the same field names and types.
15
16.. index:: string in C
17
18The Go ``string`` type is currently defined as a two-element
19structure:
20
21.. code-block:: c++
22
23 struct __go_string {
24 const unsigned char *__data;
25 int __length;
26 };
27
28You can't pass arrays between C and Go. However, a pointer to an
29array in Go is equivalent to a C pointer to the equivalent of the
30element type. For example, Go ``*[10]int`` is equivalent to C
31``int*``, assuming that the C pointer does point to 10 elements.
32
33.. index:: slice in C
34
35A slice in Go is a structure. The current definition is:
36
37.. code-block:: c++
38
39 struct __go_slice {
40 void *__values;
41 int __count;
42 int __capacity;
43 };
44
45The type of a Go function with no receiver is equivalent to a C
46function whose parameter types are equivalent. When a Go function
47returns more than one value, the C function returns a struct. For
48example, these functions have equivalent types:
49
50.. code-block:: c++
51
52 func GoFunction(int) (int, float)
53 struct { int i; float f; } CFunction(int)
54
55A pointer to a Go function is equivalent to a pointer to a C function
56when the functions have equivalent types.
57
58Go ``interface``, ``channel``, and ``map`` types have no
59corresponding C type (``interface`` is a two-element struct and
60``channel`` and ``map`` are pointers to structs in C, but the
61structs are deliberately undocumented). C ``enum`` types
62correspond to some integer type, but precisely which one is difficult
63to predict in general; use a cast. C ``union`` types have no
64corresponding Go type. C ``struct`` types containing bitfields
65have no corresponding Go type. C++ ``class`` types have no
66corresponding Go type.
67
68Memory allocation is completely different between C and Go, as Go uses
69garbage collection. The exact guidelines in this area are
70undetermined, but it is likely that it will be permitted to pass a
71pointer to allocated memory from C to Go. The responsibility of
72eventually freeing the pointer will remain with C side, and of course
73if the C side frees the pointer while the Go side still has a copy the
74program will fail. When passing a pointer from Go to C, the Go
75function must retain a visible copy of it in some Go variable.
76Otherwise the Go garbage collector may delete the pointer while the C
3ed1b4ce 77function is still using it.