Starting a new Go project is delightful.

I started a couple projects recently, a mock crypto exchange and my latest project a unicode manipulation library. But what struck me is that its really simple to get started. You need the go runtime and a GOPATH setup.

Then you specify the package and the main method and that is a valid program.

package main

func main(){}

Above is my program so far. It is a short program with a bit of exploratory code that converts strings into their unicode rune ids. Back when I mainly used Java, I would have had to setup an IDE, integrated maven and think about package structure. In Go I just have a main.go file, if I need dependencies I create a /vendor directory and usego get github.com/gin-gonic/gin to pull the dependency.

Overall the lightweightness of the Go tooling makes it very easy to build small programs.

 

 

Using pprof to examine the heap and memory usage in Golang Programs

I had some trouble getting the info I needed to setup pprof in my program. And figuring out the steps to get actionable data out of pprof. So here is my attempt to provide the minimum steps needed to use pprof.

Instrument your code

import (     
   _ "net/http/pprof"         
    ) 
func main() {     
   go func() { log.Println(http.ListenAndServe("localhost:6060", nil)) }() 
//Your program 
}

Make sure you have the above in your main.go file. This sets up a webserver that provides pprof data at the below link.

localhost:6060/debug/pprof/

Heap Memory Usage

In your shell run: go tool pprof http://localhost:6060/debug/pprof/heap This will open a cli program, enter top into the prompt.

go tool proof http://localhost:6060/debug/pprof/heap

This gives you the top 10 nodes of memory usage. It will truncate the results if most of the memory is in the top 3 nodes.

CPU time

To sample 30 seconds of cpu time, with 50ms of time ‘sampled’. go tool pprof http://localhost:6060/debug/pprof/profile

go tool proof http://localhost:6060/debug/pprof/profile

    pprof Godoc:   https://golang.org/pkg/net/http/pprof/