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.

Writing assembly in Go makes sense when you need to bypass the compiler's abstraction for specific low-level optimizations, such as implementing critical runtime primitives, handling architecture-specific instructions, or interfacing with hardware that requires precise control over registers and memory layout. Use the go tool asm command to assemble .s files into object files that can be linked into your Go package, typically for functions like runtime.mallocgc or syscall entry points where standard Go cannot express the required logic.

go tool asm -p runtime -o runtime.o runtime/asm.s

This command assembles runtime/asm.s into runtime.o for the runtime package, which is then linked into the final binary.