Member-only story
How to analyze Go code in assembly
Today, I will introduce some commonly used commands and tools for viewing Go assembly code and debugging Go programs. These tools can be used in regular situations or when engaging with colleagues or online discussions, allowing you to have an upper hand in critical moments.
For example, if a colleague claims that the first piece of code is more efficient than the second one:
package main
type Student struct {
Class int
}
func main() {
var a = &Student{1}
println(a)
}package main
type Student struct {
Class int
}
func main() {
var a = Student{1}
var b = &a
println(b)
}and they explained it in such a way that you couldn’t win the argument. What should you do? Just use a single command to generate the assembly code and expose their argument, giving them a reality check.
Generating Assembly Code with go tool
In fact, it’s quite simple. There are two commands that can achieve this:
go tool compile -S main.goor:
go build main.go && go tool objdump ./mainThe first one is for compilation, which means compiling the source code into an .o object file and outputting the corresponding assembly code.
