Using C2Go
Export C to Go
Expose defined C functions and managed records as generated Go APIs.Maintained byC2Go contributorsc2go_extern exports a defined C function through the generated package. Managed record metadata lets c2go-bind expose a matching Go-visible layout.
Export a function
#include <c2go.h>
#include <stddef.h>
#include <stdint.h>
c2go_extern int64_t sum(const int64_t *values, size_t count) {
int64_t total = 0;
for (size_t i = 0; i < count; ++i) {
total += values[i];
}
return total;
}
Prefer fixed-width integers in public APIs. long is 64 bits on 64-bit Linux and macOS targets but remains 32 bits on 64-bit Windows; do not use it for a cross-platform Go API unless that difference is intentional.
The default Go export name converts each underscore-separated segment to CamelCase and removes the underscores: sum becomes Sum, is_upper becomes IsUpper, and sqlite3_open becomes Sqlite3Open. Set the final Go import path only with c2go-clang -fc2go-package; the manifest carries it to the binder.
Use c2go_extern_as(C2GO_KEEPCASE) only when exact spelling is part of an existing contract:
c2go_extern_as(C2GO_KEEPCASE)
int ParseID(const char *text) { /* ... */ }
Declarations and definitions must agree. Put c2go_extern on the first declaration; adding it only on a later redeclaration is rejected.
Export a managed record
struct managed Point {
double x;
double y;
};
c2go_extern struct Point translate(
struct Point point,
double dx,
double dy
) {
point.x += dx;
point.y += dy;
return point;
}
The manifest records size, field offsets, and pointer layout. c2go-bind uses those facts to generate a Go-compatible type rather than guessing from host compiler defaults.
Packed or custom-aligned managed records produce C2Go warnings. The current manifest/binder path can describe explicit padding, but the resulting public layout is target-specific and must be checked in the generated Go type. An unmanaged record that contains a GC-scanned data pointer is stricter: packing or custom alignment is rejected because the conservative scanner would miss a shifted pointer word.
Names reserved by Go
An exported init or main function cannot retain a lowercase name. These names have language-level meaning in Go, so C2Go rejects an export that would collide with them.
Keep the generated package intact
Generated files include ABI/version guards and may include runtime imports, GC metadata, or native-call glue in addition to the visible declarations. Do not copy only the public-looking .go file. Keep the complete generated directory and its licensing records together.
Next, learn how memory and Go GC rules keep exported pointer layouts visible to the collector.