Mahdi.Kh
September 21, 2026
Packages and modules are both ways to organize and reuse code. Here, we get familiar with packages and modules, and learn how to use packages and modules developed by other people in our own projects.
In Go, every file that contains Go code must start with a package. Using the package keyword together with a name, we can specify the package's name.
In Go, packages are defined at the directory level. This means a directory (folder) has a name, and that name is used to access the content inside that package. Every file inside the same directory (folder) must have the same package name. It's best for the package's name to always match the name of the directory (folder) that the package's files are in.
Packages help us break code into smaller pieces and use those pieces multiple times, wherever we need them. For example, we use the fmt package to print and format data.
To use a package, we can add it to our current code using the import keyword. In the example above, we added the fmt package to the greeting package and then used it inside the Hello function.
In Go, functions, data types, and variables whose names start with a capital letter can be used outside the package they're defined in. In the previous example, we used the Printf function from the fmt package. This function's name starts with the capital letter P, so it's accessible from other packages.
There are two special packages in Go:
main
internal
main PackageEvery project meant to produce an executable (binary) as output must have a package named main, and a function named main defined inside that package. Go programs start running by executing the main function of the main package.
The main package can't be imported by other packages. If a program has no main package, it can't have an executable.
Suppose, as in the example above, we have two main files; so we can compile the project once for each of these files and end up with two executables.
internal PackageThe internal package is an access-restriction mechanism that lets certain packages be used only in a limited way. In other words, packages inside an internal directory can only be imported by packages located in that same direct parent folder of internal, or in its subdirectories.
In the example above, the contents of the crypto package can only be imported inside packages defined within the business folder. For example, the crypto package can be used inside http.go, but it can't be used inside server/web.go.
Wherever you want packages to be unusable outside of their parent package, you can put those packages inside an internal folder, as in the example above.
By following the principles below, we can do a better job naming packages:
In Go, package and module are two important concepts.
go.mod file, which defines the project's version, dependencies, and path.Using the go mod init command together with a name, you can create a module:
Running this command creates two files named go.mod and go.sum. In short:
The go.mod file specifies the module's name, the required Go version, and the project's dependencies.
The go.sum file contains checksum values for each dependency, as well as the hash of that dependency's go.mod file. This file's main purpose is to keep the program's runs and builds reproducible and secure. Every time we download a dependency, Go compares its hash with the value inside go.sum to make sure the downloaded content is exactly what was previously verified.
Using the go get command, you can install a module as a dependency. For example, running the command below installs the uuid module in the project:
And then you can import and use this module's packages:
If you no longer want to use a dependency, first remove the code that uses that dependency from your project, and then run the go mod tidy command so that dependency is also removed from go.mod and go.sum.
Previous part: Error Handling | Golang basics
Next part: Reflection | Golang basics