In Go, packages and modules play a crucial role in organizing and managing code. Here’s a breakdown of their differences and how they work:
Packages:
Definition: A package is a collection of Go source files in the same directory. It acts as a namespace for the code within it. This is what we will mostly use within our applications in class, as they are simple enough to only need packages and not modules.
Every file must define a package that it belongs to at the top of the file with the command package
.
Purpose: Packages help organize code into logical units, promoting reusability and maintainability.
It should be noted that you have to include at least one package, and it must be named main
in your application. We’ve seen this from the very first application we’ve built in Go, as well as all of our examples.
Example: The fmt
package in Go’s standard library provides functions for formatted input/output operations.
Modules:
Definition: A module is a collection of packages that are versioned and distributed together. It defines a unit of code dependency.
Purpose: Modules enable dependency management, allowing you to specify the exact versions of external packages your project relies on.
Example: You can create a Go module to manage your project’s dependencies, including third-party libraries and your own packages.
Key Differences:
Scope: Packages are the building blocks of Go programs, while modules are a way to manage dependencies between those packages.
Versioning: Modules have versioning information, enabling you to use specific versions of packages. Packages themselves do not have versions.
Organization: Packages are organized within directories, while modules are defined by a go.mod
file in the root directory of your project.
How to Use Them:
Creating Packages: To create a package, simply place Go files in the same directory and declare the package name at the top of each file (e.g., package mypackage
).
Creating Modules: To create a module, use the go mod init
command, which generates a go.mod
file to track dependencies.
Importing Packages: Use the import
keyword to import packages into your Go files, allowing you to use their exported functions and variables.
Managing Dependencies: Use the go get
command to add dependencies to your module, and the go mod tidy
command to clean up unused dependencies.
These two files need to be in separate folders, but on the same level for the following commands to work.
// mypackage/mypackage.go
package mypackage
import "fmt"
func PrintHello() {
fmt.Println("Hello from mypackage!")
}
// main.go
package main
import "mypackage"
func main() {
mypackage.PrintHello()
}
In the folder with you main package, you need to modify your go file so you can actually import the external package.
go mod edit -replace example.com/mypackage=../mypackage
Then you want to run
go mod tidy
This will create an entry with the appropriate versioning for it to import the package and allow you to use the external file.
Working with Packages and Modules in Go was originally found on Access 2 Learn