Best Practices: Effective Go
In Go, there is often only "One Way" to do things. Following the core conventions and idioms of the language—often referred to as being Gopher-like—ensures that your code is readable by any Go developer and performs optimally.
1. Naming Conventions
Go uses visibility-based naming:
- Exported: Starts with a Capital letter (
UserService). Visible to other packages. - Unexported: Starts with a lowercase letter (
userStore). Local to the current package.
Best Practice: Keep names short. Use i for loop counters, r for readers, and w for writers. Use MixedCaps (CamelCase) rather than underscores.
2. Error Handling Best Practices
- Errors are not strings: Use the
errorspackage or custom types to make your errors inspectable witherrors.Asorerrors.Is. - Don't ignore errors: Never use
_to discard an error unless you are absolutely sure it can be safely ignored. - Fail Fast: Return errors as early as possible to keep your code indentation flat.
3. Package Organization
- Flat structure: For small projects, keep all files in the root package.
cmd/: Store your application entry points (themainfunctions) in sub-directories of acmd/folder.pkg/orinternal/: Store your library code. Code ininternal/cannot be imported by other projects, ensuring your private APIs stay private.
4. Documentation: The Go Way
Documentation is built into the toolchain. Use standard comments directly above your functions and types.
// CalculateSum returns the addition of two integers.
// Exported functions should always be documented.
func CalculateSum(a, b int) int {
return a + b
}
go doc: View documentation for any package or function in your terminal.
5. Composition over Inheritance
As we’ve discussed, always prefer embedding and interfaces over trying to build deep hierarchy trees. This is the most crucial principle for building maintainable Go systems.
Interview Pro-Tips: What is "Effective Go"?
If an interviewer asks about your coding style:
- The Answer: Mention the official "Effective Go" document and the "Go proverbs." Highlight that you prioritize Simplicity, Explicitness, and Readability over "clever" or "magical" code. Mention that you use
gofmtorgoimportsto ensure your formatting is always standard.
Technical Summary
Simplicity: Code should be boring and easy to read.Explicitness: No hidden magic or global state.Tooling: Letgofmthandle the formatting debates.Interface: Build small, focused interfaces that do one thing well.