Go Module 9
Packages, modules and dependencies interview questions
Interview questions on packages, modules and dependencies: warm-ups, then what each harder question tests and how a good answer starts.
Warm-up questions
Short questions with short answers, each answered in full.
-
Fresher
What makes a name in a Go package exported?
Answered in the lesson Packages, exported names and initialisation
Questions with model answers
What each question tests, the first step of a good answer and the follow-ups to expect.
-
Mid to senior
Your module has a package named helpers, meant only for its own commands. Another team now imports it and complains whenever you change it. What would you have done differently, and how do you fix it now without breaking them overnight?
- What it tests
- Knowing that exported names in an importable package are public API, the internal/ rule and whom it admits, and a migration that gives the other team a supported path.
- First step
- Separate two questions: what the other team needs, and what you never meant to support. Every exported name of a package that other modules can import is public, so the package should have lived under internal/ from the start, where the go command refuses imports from outside the tree.
- Follow-ups
-
- Why is turning the names to lower case a poor fix?
- How does the go command decide who may import an internal package?
- When is a separate, versioned module the better answer?
From the lesson Packages, exported names and initialisation
Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.Opening the model answers with your pass…
Model answers open with any MySmartCoPilot pass.
- Pro pass · 1 week₹79$2.99
- 30 days₹179$5.99
- 1 year₹1,699$45
One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.
Passes can’t be bought right now, so the model answers can’t be opened at the moment. If you have a pass, enter your licence key.
It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).
In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Fresher to mid
A package opens its database connection in an init function and panics when the database is down. Since then, the unit tests of unrelated packages fail on every laptop without a database. Explain why, and how you would restructure the package.
- What it tests
- Knowing when init runs and for which programs, that it cannot return an error, and replacing hidden setup with an explicit constructor that returns one.
- First step
- Follow the imports. An init function runs in every program and test binary that imports its package, directly or through other packages, before any test starts, and it can report a failure only by panicking. So every test binary that reaches this package needs a database, whether its tests query one or not.
- Follow-ups
-
- Why do the tests of packages that never query the database fail?
- What is init still good for?
- How do you share one connection without a package-level global?
From the lesson Packages, exported names and initialisation
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes