Boone Putney bio photo

Boone Putney

Software Development
Random Musings
Austin, Texas

HumanPlanet Soleer

Email LinkedIn Github

One of the great features of Go is its testing package. The testing package has streamlined our unit-testing and test-driven development.

One addition we found useful was the implementation of stack-traces on certain types of errors. Since our testing suite consisted of a lot of functions that were called repeatedly, tracing out the stack made tracking down failed tests much easier.

Here’s how to implement a stack trace in Golang:

Golang log stack trace Example

 1 import (
 2 	"log"
 3 	"runtime"
 4 	"testing"
 5 )
 7 func (t *testing.T) TestFunction(err bool){
 8 	if err {
 9 		//stack trace
10 		var stack [4096]byte
11 		runtime.Stack(stack[:], false)
12 		log.Printf("%s\n", stack[:])
14 		//fatal error
15 		t.Fatalf("Error!")
16 	}
17 }