tezvyn:

Passing Go/Rust callbacks to a C library

Source: interviewintermediate

WHAT IT TESTS: FFI callback mechanics. OUTLINE: C needs a plain function pointer; in Go use //export with cgo, in Rust an extern "C" fn; carry state via a void* user-data param.

WHAT IT TESTS: whether you can bridge a C function-pointer callback from Go or Rust safely. ANSWER OUTLINE: C wants a bare function pointer with C calling convention. In Go, you define an //export-ed function and pass it via cgo, often threading state through a registered handle since you cannot pass Go closures directly. In Rust, you pass an extern "C" fn; capturing closures need a separate void* user-data pointer cast back inside. Key risks: unwinding across FFI is UB, and state lifetime must outlive the C call.

Read the original → interview

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

Passing Go/Rust callbacks to a C library · Tezvyn