Unsafe Ffi
20 articles
How to Call C from Go with Cgo
Call C functions from Go by adding a C preamble comment, importing "C", and using the C. prefix to invoke functions.
How to Call Python from Go (and Vice Versa)
Call Python from Go using os/exec or cgo, and call Go from Python using subprocess or ctypes for shared libraries.
How to Convert Between Pointer Types with unsafe
Convert between pointer types in Go by casting through unsafe.Pointer to bypass type safety.
How to Read Go Assembly Output
Run go tool asm -S file.s to view the assembly source and generated machine code.
How to Understand Go's Calling Convention
Go's calling convention is compiler-managed, but you can control related runtime behaviors using GODEBUG settings or go:debug directives.
How to Use Compiler Intrinsics in Go
Go compiler intrinsics are internal optimizations applied automatically by the compiler during the SSA phase to replace standard code with efficient machine instructions.
How to Use go:linkname for Accessing Private Runtime Functions
Use the //go:linkname directive to create a public alias for private runtime functions by linking a local name to the external package path.
How to Use go:noescape and go:nosplit Directives
Use //go:noescape to optimize pointer handling and //go:nosplit to prevent stack growth interruptions in critical Go functions.
How to Use go tool objdump and go tool compile -S
Use go tool objdump to disassemble binaries and go tool compile -S to view generated assembly from source files.
How to Use SIMD Instructions in Go Assembly
Use SIMD in Go by writing architecture-specific vector instructions and register names directly in .s assembly files.
How to Use syscall Package in Go
Use the syscall package in Go to perform low-level OS operations like file locking with syscall.Flock.
How to Use unsafe.Pointer in Go
Use unsafe.Pointer to cast Go variable addresses for C function calls via cgo, ensuring the Go object remains referenced to prevent premature garbage collection.
How to Use unsafe.Sizeof, Alignof, and Offsetof
Use unsafe.Sizeof, Alignof, and Offsetof to get the byte size, alignment, and field offset of Go types at compile time.
How to Write Assembly Functions in Go
Write assembly functions in Go by creating a `.s` file in your package, defining a global symbol with `TEXT`, and calling it from Go using `//go:nosplit` and `//go:linkname` or `//go:export` depending on direction.
Introduction to Go Assembly (Plan 9 Assembly)
Go Assembly is a low-level language compiled by `go tool asm` to create object files for high-performance or hardware-specific tasks.
What Is Cgo and How to Call C Code from Go
Cgo is a tool for calling C functions from Go by wrapping C headers in comments before importing the special "C" package.
What Is the unsafe Package in Go
The unsafe package in Go allows direct memory access and type manipulation, bypassing standard type safety for performance or C interoperability.
What Is unsafe.Slice and unsafe.String in Go
unsafe.Slice and unsafe.String convert between byte slices and strings without copying data for performance.
When Writing Assembly in Go Actually Makes Sense
Write assembly in Go for critical runtime optimizations, hardware interfacing, or when the compiler cannot generate the required machine code.
Why You Should Almost Never Use unsafe in Go
Avoid using the unsafe package in Go to prevent memory corruption and security risks, reserving it only for critical performance optimizations where no safe alternative exists.