runtime/cgo

Guided tour · Runtime & Debug · pkg.go.dev →

Support package for cgo. Most programs import this transparently via `import "C"`. Its direct API is a handle system for passing Go values to C.

Handle — pass a Go value through C

Safely round-trip a Go value across a C call that stores an opaque pointer.

NewHandle / Value / Delete

// Package foo imports "C" and calls some C API that takes void*.
obj := &MyStruct{Name: "Ada"}
h := cgo.NewHandle(obj)

// Pass uintptr(h) to C as void*. Later, in a Go callback:
v := h.Value().(*MyStruct)

// Free when done — handles are never collected automatically:
h.Delete()