Advanced Golang

Modules Packages and Imports

Naming packages

In this lesson, we will explore best practices for naming your Go packages. Choosing descriptive package names is crucial because the package name becomes part of the code you reference throughout your application. Clear and meaningful package names improve code readability and maintainability.

Why Descriptive Package Names Matter

Consider a scenario where you have two helper functions: one for extracting names from a string and another for formatting names. Instead of grouping both functions in a generic "util" package (e.g., util.ExtractNames and util.FormatNames), it's more informative to organize them into distinct packages that convey their purpose. This approach leads to cleaner and more intuitive code.

For example, you can create:

  • An extract package that includes a function named names.
  • A format package that also includes a function named names.

Even though both functions share the same name, their package prefixes disambiguate them. When imported, you would reference them as:

extract.names
format.names

This naming convention makes it clear which functionality is being used and helps avoid ambiguity in your code.

Note

Avoid reusing the package name in the function names. For example, if your package is named extract, do not name your function extractNames since the package context already implies the functionality.

The image is a slide titled "Naming packages," explaining how to extract names from a string and format a string, with examples like "extract.Names" and "format.Names."

Organizing Files and Directories

Every Go file within a directory must begin with the same package clause. As a best practice, the package name should match the directory name containing the files. This convention ensures that the package's purpose and origin are instantly recognizable.

In some cases, this guideline is relaxed. A common exception is the use of the special package name main. The main package serves as the entry point for a Go application and cannot be imported. Therefore, it is acceptable for the directory name to differ from the package name without causing confusion in import statements.

Warning

Be cautious not to overcomplicate your package structure. While organizing by functionality is beneficial, ensure that each package remains focused and doesn't become a catch-all for unrelated functions.

That concludes this lesson on naming packages in Go. We look forward to exploring more topics with you in the next lesson.

Quick Reference

AspectBest PracticeExample
Package NamingUse descriptive names that reflect functionalityextract, format
Function NamingAvoid redundancy; do not include the package name in the function namenames in both extract and format packages
File OrganizationEnsure all files in a directory start with the same package clausePackage name = directory name
Special Case - Main PackageUse the main package as the entry point and understand its unique propertiespackage main

Happy coding and continue building robust and maintainable Go applications!

Watch Video

Watch video content

Practice Lab

Practice lab

Previous
Package name collision