Skip to content
tezvyn:
Menu

Purpose of Go import C and Rust equivalent mechanism

Source: pkg.go.devEasyHow cards are made

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's really being asked

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.

The full answer

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.

The mistakes people make

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.

What usually comes next

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.

A 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.

Interview question

Which accurately describes a key difference between Go's cgo and Rust's FFI for calling C functions?

  • a.Both languages automatically link C libraries through a special import directive without extra build configuration.
  • b.Go requires the unsafe keyword on every C function call, while Rust calls C through a standard use statement without unsafe.
  • c.Go uses a preamble comment before import "C" to include C headers, while Rust declares C functions in an unsafe extern "C" block.Correct
  • d.Go's import "C" is a normal package fetched from the module proxy, while Rust uses an unsafe extern "C" block.
Why?

Option C is correct because Go uses a preamble comment immediately before import "C" to include C headers, while Rust declares C functions inside an unsafe extern "C" block. Option D is a tempting distractor because import "C" is not a normal package from the module proxy but a special cgo directive recognized by the compiler.

Just read this? Test yourself on what you have been reading.

Read the original → pkg.go.dev

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on go — each one lists the topics its interview covers.

See open roles