Unsafe, Cgo, and FFI

Read Unsafe, Cgo, and FFI as a Go habit shift, with links to the Musi Book definition.

A Go reader brings habits from packages, structs, slices, nil, multiple returns, interfaces, goroutines, channels, and explicit errors. That helps with small named operations and direct data flow, but the Musi page asks a narrower question: what contract should this native and unsafe boundaries example make visible?

go
// #include "clock.h"
import "C"

func nativeClockMs() int {
    return int(C.clock_ms())
}

Reading Unsafe, Cgo, and FFI from Go

On the Musi side, Musi foreign declarations use foreign "c" let ... or a parenthesized foreign group, and unsafe calls stay inside unsafe { ... }. Read the shared example through Go eyes: keep the useful instinct, then let Musi name shape, behavior, absence, and outside work in separate places.

False friend

Do not invent a C-like foreign block or spread unsafe assumptions through ordinary Musi code. For a Go reader, the trap is using absence or failure as a side channel because Go makes that cheap; Musi class is closer to an explicit interface constraint with instances; it is not a struct and not a method set attached by package convention.

When this pays off

Use this shape when a clock, driver, C library, host VM, or platform handle must cross into Musi. The Go instinct still helps here: Keep the Go habit of writing the small thing first and naming package boundaries clearly.

Keep close