katzien / go-structure-examples

Examples for my talk on structuring go apps
MIT License
2.34k stars 286 forks source link

Question about Service interface in service packages #15

Open soqt opened 4 years ago

soqt commented 4 years ago

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?

godwhoa commented 4 years ago

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 4 years ago

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!

lewislbr commented 3 years ago

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

godwhoa commented 3 years ago

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