In Go, modules and packages are two distinct but related concepts. Understanding the difference is essential when working with Go projects, especially as they grow.
A package is a collection of Go source files in the same directory that are compiled together. It defines reusable code that can be imported by other Go files.
package <name>
.fmt
, math
, net/http
, etc.// file: mathutil/add.go
package mathutil
func Add(a, b int) int {
return a + b
}
And use it in another file:
import "your-module-name/mathutil"
result := mathutil.Add(2, 3)
A Go package does not need to have the same name as its source directory — but it's a common convention to keep them the same.
Go allows you to use any valid package name inside a source file, regardless of the directory name.