Neither one nor Many

December 18 2018

Testing and coverage


  • go test
  • go test -cover

    go test -cover -coverprofile=c.out go tool cover -html=c.out -o coverage.html

If you have main.go, add main_test.go:

package main

import "testing"

func TestSum(t *testing.T) {
    total := Sum(5, 5)
    if total != 10 {
       t.Errorf("Sum was incorrect, got: %d, want: %d.", total, 10)

More info here:


Source: (by Ross Cox)

var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
func main() {
    if *cpuprofile != "" {
        f, err := os.Create(*cpuprofile)
        if err != nil {
        defer pprof.StopCPUProfile()

Build an capture profiling data

  • go build && ./myprogram
  • go tool pprof myprogram

Commands available are:

  • top5
  • top10
  • top10 -cum
  • list main
  • list myfoobar.function.func1

There are also very similar flags for memory profiling. I didn't try this myself yet, check the blog I got this from in the first place.

Cheatsheets Comments (0)

Leave a Reply

Comment may not be visible immediately, because I process everything manually.**

**) I plan to automate this.., but it's on my ToDo since for ever..

Ray Burgemeestre
february 23th, 1984

C++, Linux, Webdev

Other interests:
Music, Art, Zen