#TIL | #Go

July 3, 2021 | 11:38

go tool dist list

Imagine that you want to cross-compile your Go binary from your Mac for a Linux machine that runs on arm64 architecture. With Go, it’s a matter of specifying the GOOS and GOARCH environment variables for go build command. GOOS=linux GOARCH=arm64 go build main.go You don’t always know/remember what values for GOOS and GOARCH or you are not even sure if Go supports the target you desire. This is where the command go tool dist list comes in handy, when you run it it will list all the GOOS/GOARCH combinations available for the Go version that you are running. Read more

May 28, 2021 | 04:50

Go Performance Tools Cheat Sheet

Go has a lot of tools available for you to understand where your application might be spending CPU time or allocating memory. I don’t use these tools daily so I always end up searching for the same thing every time. This post aims to be a reference document for everything that Go has to provide. We’ll be using https://gitlab.com/steve-blog/go-performance-tools-cheat-sheet as a demo project and there are 3 implementations of the same thing, one more performant than the other. Read more

April 3, 2021 | 00:00

import "context"

What can you do when you import "context" inside of your go project? Looking at the source code it’s a fairly small package and provides a small api. We also see this package imported almost everywhere and the standard library also uses it. context provides the following functionality: Cancellation Context scope values Deadlines/Timeouts There are two things that you need to keep in mind when you are using context: The context. Read more
#TIL | #Go

July 21, 2020 | 20:03

Go Trace Shortcut

You can use the go trace tool to look at the CPU traces to see how your program is behaving for example. package main import ( "fmt" "os" "runtime/trace" ) func main() { f, err := os.Create("trace.out") if err != nil { panic(err) } defer f.Close() err = trace.Start(f) if err != nil { panic(err) } trace.Stop() fmt.Println("hello") } When you run the program you will see a new file trace. Read more