Assembly inspection and build modes
Assembly inspection and build modes
Compiler output is evidence about one toolchain, target and build configuration. Use go build -o demo main.go, then go tool objdump -s "main.twice" demo to inspect a compiled symbol. //go:noinline keeps this tiny demonstration as a separate function in the current compiler; it is an implementation directive, not portable Go semantics. Do not compare exact opcodes across versions.
package main
import ( "fmt")
//go:noinlinefunc twice(n int) int { return n + n }func main() { fmt.Println(twice(21))}First establish correct behavior; then look at instruction selection and calls. Optimizations may remove symbols or transform a calculation. The listing is not a promise of source ordering or stable performance. For source assembly use the Go assembler conventions, build constraints and tests. Go assembly uses architecture-specific conventions and pseudo-registers such as FP and SB.
Expected output:
42Choose a build mode for the consumer
package main
/*#include <stdint.h>*/import "C"import "fmt"
//export AddSmallfunc AddSmall(a, b C.int32_t) C.int32_t { return a + b }func main() { fmt.Println(AddSmall(19, 23)) }This example demonstrates a bounded addition; callers must keep the sum representable in int32. On supported targets, go build -buildmode=c-shared -o libdemo.so main.go creates a shared library and C header; -buildmode=c-archive creates an archive and header. A C host must link or load the artifact and call AddSmall using the header, rather than execute main. Build modes such as pie and plugin have target-specific constraints; Go plugins additionally require tightly matching dependencies/toolchains. The .so filename is a Linux example. The companion lab tests a real C host: copy fixtures/native-host/client.c beside the generated libdemo.h and run gcc client.c ./libdemo.so -pthread -ldl -lm -o client, then ./client in that directory. For the archive mode use ./libdemo.a instead. The expected output is C host: 3 cases passed.
Expected output:
42Try it locally
Write a non-inlined clamp function accepting an int, limiting it to 0..10. Run cases below, within and above the range. Inspect the compiled symbol using objdump rather than predicting the exact instruction sequence.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Native interoperability 05: Assembly inspection and build modes
Check the lesson objective before moving on.
What does an objdump listing prove?
Which artifact normally accompanies a c-shared Go library?