naesnaes

Golang

Heap Escape Analysis in Go

Warning

This page is extremely rough and will contain incorrect code and lack of explanation.

I want to figure out if my go program is making any allocations to the heap.

go test -run none -bench . -benchtime 10s -benchmem

Output:

goos: darwin
goarch: arm64
pkg: src.naesna.es/aoc/go/2024
cpu: Apple M1 Pro
BenchmarkDay1Part1-10              56686             62211 ns/op           83192 B/op         26 allocs/op
PASS
ok      src.naesna.es/aoc/go/2024       4.405s

You can also get a memory profile

-memprofile mem.out

Then use go tool pprof to check allocation space.

What should I name my go mod?

When I do go mod init, what should I init my module as? It seems like convention is to set the go mod name to the page of your repo in GitHub. But why?

Looking at the gomod docs:

The module path must uniquely identify your module. For most modules, the path is a URL where the go command can find the code (or a redirect to the code). For modules that won’t ever be downloaded directly, the module path can be just some name you control that will ensure uniqueness. The prefix example/ is also reserved for use in examples like these.