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.


📦 Packages in Go

✅ What is a package?

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.

📌 Key points:

🔧 Example:

// 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.


✅ What's Allowed?

Go allows you to use any valid package name inside a source file, regardless of the directory name.

Example: