katzien/go-structure-examples

Question about Service interface in service packages

soqt opened this issue · 4 comments

soqt commented

Hi Kat, after watching your talk, I found your project structure is very inspiring and I'm migrating my project to this structure now. I have a question. Why do you want to create a Service interface in every service package instead a Service struct? and all functions with (s *Service) automatically bind to the struct. Is there any special use case for Service interface?

With a Service interface for each service you can create mocks to make testing easier wherever they are used(eg. handlers).
You also get the benefit of being able to wrap your services with logging, tracing, metrics middleware.

soqt commented

With a Service interface for each service you can create mocks to make testing easier wherever they are used(eg. handlers).
You also get the benefit of being able to wrap your services with logging, tracing, metrics middleware.

Thanks for your answer. That makes much sense!

Related to this: shouldn't the service constructors return the struct?

I guess it's due to service struct not being exported. But yeah "accept interfaces, return structs" is a common saying.