mediumDesignCodingMidSeniorStaff
Go gives you no project layout and no dependency-injection container. How do you structure a service so it stays testable as it grows?
5 min readUpdated 2026-08-15
Answer
Short answer
Wire dependencies explicitly through constructors from one place in main, define narrow interfaces where they are consumed rather than implemented, and keep handlers thin enough that the logic beneath is testable without a server.
Was this answer helpful?
Likely follow-ups
- Where does the interface for your database live, and why there?
- How do you test a handler without starting a server?
- When is a global logger acceptable and when is it not?
- What breaks first when the package tree gets deep?
Related questions
- Your function returns nil on the success path, the caller checks err != nil, and the check fires anyway. What happened?hardAlso on interfaces and golang5 min
- Messages arrive with a type field and each type needs a different object built to process it. How do you design the creation so you do not end up with a growing switch statement?mediumAlso on dependency-injection6 min
- Design a parking lot system — you have 90 minutes, working code at the end.hardAlso on interfaces6 min
- You have read access to the customer's production database and there is no staging environment. How do you develop and test without putting their system at risk?hardAlso on testing6 min