tezvyn:

Purpose of Go import C and Rust equivalent mechanism

AI-drafted, machine-checkedSource: pkg.go.devbeginner

This tests FFI entry points: Go's import "C" activates cgo to reference C symbols directly, while Rust uses an unsafe extern "C" block to declare external functions. A red flag is calling either a normal import or omitting unsafe in Rust.

WHAT THIS TESTS:

This question checks whether you understand how two modern systems languages cross the boundary into C code. The interviewer wants to see that you know Go uses a special pseudo-package called C to activate cgo, and that you know Rust uses a dedicated unsafe extern block to declare foreign function signatures. It also tests whether you appreciate the safety implications, especially Rust's explicit unsafe requirement versus Go's more implicit cgo integration.

A GOOD ANSWER COVERS:

First, explain that in Go import "C" is not a real package from the module proxy but a directive recognized by the cgo tool. It unlocks the ability to write Go code that refers to C types, variables, and functions as if they lived in a package named C. Second, mention that if the import is immediately preceded by a comment, that comment becomes the preamble where you can write C headers, variable declarations, and cgo directives like CFLAGS or LDFLAGS. Third, for Rust, state that the equivalent mechanism is an unsafe extern "C" block containing function signatures with no bodies. The extern "C" part specifies the C calling convention, and unsafe is required both at the declaration site and at every call site because the Rust compiler cannot verify C code. Fourth, note that Rust also needs linking, often via a build script or cargo configuration, to actually connect to the C library, whereas Go's build tool handles much of this automatically when cgo is enabled.

COMMON WRONG ANSWERS:

A major red flag is saying that import "C" is a normal Go package you can find on pkg.go.dev. Another is claiming Rust calls C through a simple use or import statement without any unsafe keyword. Some candidates also forget that Go's preamble comment must appear immediately before import "C" with no blank lines, or they think any comment in the file works. In Rust, omitting the calling convention extern "C" or forgetting that the block itself must be unsafe are both signs of shallow FFI knowledge.

LIKELY FOLLOW-UPS:

The interviewer may ask how you pass pointers between Go and C and what the cgo pointer-passing rules are. They might ask how Go manages memory when sharing a slice with C, or how Rust's ownership system interacts with raw pointers from C. You could also be asked to compare build complexity, such as how Go uses CFLAGS and LDFLAGS in the preamble while Rust often uses a build.rs script to invoke the cc crate or pkg-config.

ONE CONCRETE EXAMPLE:

Imagine you need to call the standard C function puts from both languages. In Go, you would write a preamble comment that says // #include <stdio.h> immediately followed by import "C". Then in Go code you can call C.puts. In Rust, you would write unsafe extern "C" { fn puts(s: *const c_char); } and then invoke it inside an unsafe block with a null-terminated C string. The Go version hides the unsafe nature behind cgo, while the Rust version forces you to acknowledge it at every step.

Read the original → pkg.go.dev

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.