Go Tools — Trace

Have you ever used go tool trace command ?

If you have not, this is the right place to get grounded.

But before going for it, I need to introduce a concept “Go execution tracer”

What the hell is the Go Execution Tracer?

As the name suggests, it’s a tracer that tracks down all activities about “Execution”.

What do you mean by “Execution” here ?

It’s a collection of every single execution happens in a program such as how many goroutines are executing, for how long, and when are they blocking for what ? and how does Garbage Collector affect execution of individual goroutine? You can literally have fine-grained granular information about all those things by leveraging “Go Execution Tracer”.

So, how can I use the Go Execution Tracer? and what does it have to do with the go tool trace command you’re talking about?

Hang on bro, let me talk you through that now with an example.

Let’s take a look at this example code.

This is one way of generating trace data out of a Go application.

using “runtime/trace” package, you can start or stop tracing.

so, line 9 starts tracing and output to standard error.

and once the main function returns, the deferred function trace.Stop() will be called to stop tracing.

But to output the trace data to a file, you need to run this command

go run main.go 2> trace.out

then, you will get this output in your filesystem.

➜  tracer git:(master) ✗ go run main.go 2> trace.out 
➜ tracer git:(master) ✗ tree .
├── main.go
└── trace.out
0 directories, 2 files
➜ tracer git:(master) ✗

Okay, I’ve been sitting tight and waiting to use the go tool trace but you’re testing my patience…

Calm down bro, now you can go head and run the command. here you are.

➜  tracer git:(master) ✗ go tool trace trace.out 
2022/09/15 11:26:13 Parsing trace...
2022/09/15 11:26:13 Splitting trace...
2022/09/15 11:26:13 Opening browser. Trace viewer is listening on

Cool! what’s happening here ? it’s opening browser ?

Yes, yes. Check out a popped-up window and see what it contains.

Cropped image

You may see something like this.

For the simplicity, let’s just briefly talk about “View trace” first today.

As the title says, it’s going to give you all the details about event timelines for all running goroutine. Clicking through it shows you this.

View trace

Because the the app ran very short, the duration of trace data is also short.

Now, you have all the trace data to understand what’s happend with your application, which means you know now what the “go tool trace” does.

About comprehending the data in the chart, I will continue on the next article.

Stay tuned.




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store


I love problem solving and hate repetition of tedious tasks. I like automating, streamlining, optimizing, things.