Go interoperates with other languages mainly in four ways: cgo to call C from Go, -buildmode=c-shared to expose Go functions to C, Python, Rust and others, network protocols such as gRPC and HTTP between separate processes, and WebAssembly. Choose the loosest coupling that meets your performance needs; a network boundary is usually safer than in-process FFI.
Every in-process option crosses a language boundary with a cost in speed, memory safety and build complexity. Network and process boundaries are slower per call, but isolate crashes and keep builds simple.
| Approach | Direction | Best for | Main cost |
|---|---|---|---|
| cgo | Go calls C | Existing C libraries, system APIs | Slower calls, needs a C compiler, harder cross-compiling |
c-shared or c-archive | C, Python, Rust and others call Go | Reusing Go code from another runtime | C-style API only, two runtimes in one process |
| gRPC or HTTP | Any to any | Services in different languages | Network latency, schema management |
| Subprocess with stdin/stdout | Any to any | CLI tools, simple pipelines | Process start cost, text or JSON protocol |
| WebAssembly | Go in browser, or Wasm modules in Go | Sandboxed plugins, browser code | Limited system access, larger binaries |
cgo lets Go code call C functions when you put a C preamble in a comment directly above import "C". There must be no blank line between them.
C.CString allocates C memory that the Go garbage collector does not manage, so you must free it. Go strings and slices are not C types, so convert them explicitly. Follow the cgo pointer-passing rules: Go memory passed to C must not contain Go pointers, and C must not keep a Go pointer after the call returns.
Practical consequences of cgo:
CGO_ENABLED=0 disables it and gives a fully static binary, which is easier to cross-compile. Use pure-Go libraries where possible.#cgo LDFLAGS: -lname and #cgo CFLAGS directives, or pkg-config.extern "C".With -buildmode=c-shared, Go produces a shared library and a C header. Mark each exported function with //export, and keep the signature to C-compatible types. The package must be main with an empty main function.
Build it and call it from Python with ctypes:
The library embeds the Go runtime, with its own scheduler and garbage collector. Do not pass Go pointers out of the library, and free any memory that you hand to the caller. On Windows the output is a .dll, and on macOS a .dylib. Use -buildmode=c-archive for a static library.
The original approach of calling the Python C API through cgo to run Python functions is possible but fragile. If Go needs Python, call it as a separate service or subprocess instead.
For services in different languages, define a contract and talk over the network.
net/http suit public APIs and browsers.os/exec to start a program and talk over standard input and output.Never build command arguments from untrusted input without validating them, and always use a context timeout so a stuck child process cannot hang your program.
Go compiles to WebAssembly with GOOS=js GOARCH=wasm for browsers and GOOS=wasip1 GOARCH=wasm (Go 1.21 and later) for WASI runtimes. To run Wasm modules written in other languages from inside Go, use a pure-Go runtime such as wazero, which needs no cgo and sandboxes the module.
Go also targets Android and iOS through gomobile, which generates Java, Kotlin, Objective-C and Swift bindings for a Go package.
Calls into C cost more than ordinary Go calls, in the range of tens of nanoseconds or more depending on the version and platform. That is fine for coarse-grained calls and wasteful in tight loops.
Yes. Build a c-shared library and load it with ctypes or cffi, or run Go as a separate service. A service is easier to deploy and debug.
Wrap the C++ API in a C interface declared extern "C" and call that wrapper through cgo.
It complicates it, because you need a C cross-compiler for the target. Using CGO_ENABLED=0 and pure-Go dependencies avoids the problem.
Go interoperates through cgo for calling C, c-shared libraries for being called by other languages, gRPC or HTTP for services, subprocesses for helper tools and WebAssembly for sandboxed code. Prefer network boundaries when you can, keep in-process calls coarse and respect the cgo pointer rules and memory ownership.