Sitemap
huizhou92

huizhou92

Golang backend engineer. Specializes in cloud, data, and security. Lifelong learner. Efficiency enthusiast.

Member-only story

How to analyze Go code in assembly

6 min readJan 16, 2024
Press enter or click to view image in full size

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.go

or:

go build main.go && go tool objdump ./main

The first one is for compilation, which means compiling the source code into an .o object file and outputting the corresponding assembly code.

--

--

huizhou92
huizhou92

Written by huizhou92

Golang backend engineer. Specializes in cloud, data, and security. Lifelong learner. Efficiency enthusiast.