Clean Packages

Writing effective packages requires thoughtful design. Follow these rules of thumb to keep your codebase clean.

1. Hide Internal Logic (Encapsulation)

Expose a small, clear API to your users and keep the “dirty work” private (unexported). The application using your package shouldn’t care how a result is calculated, only what the result is.

2. Don’t Change APIs

A well-designed library has a stable API. Avoid changing the signatures of exported functions, as this creates breaking changes for everyone using your package.

3. Don’t Export from main

The main package is the entry point, not a library. There is no reason to export functions, types, or variables from it.

4. Packages Shouldn’t Know About Dependents

A package should be generic. It should never contain logic specific to a particular application that happens to be using it. This ensures the package remains truly reusable.

5. Package Name == Directory Name

Always keep your package names consistent with their folder structure to avoid confusion during imports.