DEV Community

Discussion on: Working with Context in Go

Collapse
 
corymcdonald profile image
Cory McDonald

Great article George! Context is the thing I struggle with Go, I feel like I understand all the different types a little better now!

I find it particularly interesting that my teammates are moving to put our database, kafka, and logger connections into the Context to consume it later. When I read that it wasn't recommended by the articles you read I was shocked!

One of the reasons we decided to switch was that we were consuming those resources from a service object. When we got to testing and mocking those items it became really difficult to interface with. One of the questions that I never can find the answer for is how to best set up an API that makes it easy to interface with external services and then test them.

Thanks for writing!

Collapse
 
georgeoffley profile image
George Offley • Edited

Hey Cory,

Thanks for reading. I'm glad you got something out of it. To elaborate a little, when doing my research I came across a lot of articles which suggested not passing along critical assets via context. I find that the ability to do that makes a context far more useful. That said I'm sure there are ways to get in trouble doing so. Even if you look into the context source code they flat out tell you not to do that.

Context Source

However, point out a system which is coded perfectly to a language specification. Everyone's needs are different. So doing what you described probably won't sink you but it might be better to store that stuff in a new class to be reloaded when needed. I'm not a genius so I'm sure there are plenty of use cases for it regardless of what some guy on the internet thinks.

That might be your next project is seeing how to be best set up external services to be used from system to system. I might look more into it.

Thanks again for reading.