Tuesday, 29 May 2012

Straightforward, Powerful, Standard Library

Whilst testing a Go application to destruction I was manufacturing various scenarios and needed to discover where exactly each goroutine was in its processing. After a quick scan of the documentation, and 5 minutes keyboard bashing I was able to add a couple of simple functions to a utility package and enable applications to dump their current stack when signalled.

func ListenForPrintStackSignal() {
    sigCh := make(chan os.Signal, 1)
    go func(sigCh chan os.Signal) {
        for _ = range sigCh {
            log.Print("Request to print stack")

func PrintStack(all bool) {
     //arb sized buffer
     buffer := make([]byte, 10240)
     numB := runtime.Stack(buffer,all)
     log.Print(fmt.Sprintf("%v current goroutines",runtime.NumGoroutine()))

Go's standard library is by far the most powerful and striaghtforward set of builtins I have used. Everything seems to be in place (or planned for) to quickly plug functionality together without having to jump through hoops or battle oddly named methods with bizare signatures. The productivity kick that comes with using this standard library is one of the reasons that we will see an exponential uptake of the Go language in the coming months and years.

Thanks to @rogpeppe for the golfing tips.