🐹 Go (Golang)
Advanced
How do you profile a Go program with pprof?
Answer
The net/http/pprof package exposes profiling endpoints when imported: import _ "net/http/pprof". Access /debug/pprof/ in a browser or use go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30 for a 30-second CPU profile. Use go tool pprof http://.../heap for memory allocations. The interactive pprof shell lets you run top, list funcName, and web (generates a flame graph in the browser). For benchmarks, add -cpuprofile and -memprofile flags. Key metrics: CPU time by function, heap in-use bytes, goroutine count, and allocation rates. Profiling is essential before any optimization — measure first, then fix.
Previous
What are generics in Go and how do they work?
Next
What is the unsafe package and when is it appropriate to use it?