Richard Bucker

golang context

Posted at — Apr 8, 2015

There is no single use-case that defines the best time to use a “context”. In one of the larger programs I wrote, I used a context in order to log and timestamp the entry and exit point of every function/method in the callstack. I could have gone deeper to reflect on the current function and it’s parameters too but I stopped with the trace, transaction id, and the duration/elapsed time.The the golang authors created their own “Context”; documented here and written about here. While it is said that Google implements a “Context” parameter in all of it’s internal tools their sense of a context is for a different purpose. Mostly trapping elapsed time, canceling long running tasks and it’s child goroutines.Frankly it’s a little more complicated than that. I read through the sample code, the package code and the article and other than being able to trap a timeout I’m still digging my way out.Here is my samplesocket project. You are welcome to contribute. Before the haters warm up their engine; this is not code I would run in production or even let out of the barn to play with Wilbur. It’s just some code to start a conversation about Context.