Go modules and packages help you split a growing program into tidy folders that are easy to reuse and share. In this post, you will create a module, build your own package and add a library from the internet. Each step comes with commands and code that you can run yourself.
You already ran go mod init once in the guide to install Go and run your first program. This time, however, you will see what it really does. You will also export functions, so a refresher on writing and calling functions in Go will help.
What Are Go Modules and Packages?
A package is a folder of Go files that work together, such as fmt or strings. A module is a collection of packages with one name and one version. In short, packages organise code, while Go modules manage the whole project and its dependencies.
For example, think of a library building. The module is the building, each package is a shelf, and each file is a book on that shelf. So when you import a package, you are picking a shelf to borrow from.
Creating Go Modules with go mod init
mkdir hello
cd hello
go mod init example.com/hello
# go: creating new go.mod: module example.com/hello
First, the name you pass is the module path. For a real project, people usually use their repository address, such as github.com/yourname/hello. Then every package inside starts with that path.
Understanding the go.mod File
module example.com/hello
go 1.24.7
In addition, this small file sits at the root of your project. Specifically, it records the module path and the Go version. Later, it also lists every outside library your code needs.
Packages: Organising Your Code
Creating Your Own Package
// greet/greet.go
package greet
import "fmt"
func Hello(name string) string {
return fmt.Sprintf("Hello, %s!", name)
}
func secret() string { return "hidden" }
// main.go
package main
import (
"fmt"
"example.com/hello/greet"
)
func main() {
fmt.Println(greet.Hello("Smita")) // Hello, Smita!
}
First, create a folder called greet and start its file with package greet. Then import it with the module path plus the folder name. Finally, the diagram below shows how the pieces fit together.

One module holds two packages, and main imports greet using the module path.
Exported vs Unexported Names
Names that start with a capital letter, like Hello, are exported, which means other packages can use them. In contrast, lowercase names, like secret, stay private to their own package. So greet.secret() fails with “undefined: greet.secret”.
Importing Packages
For instance, standard library packages, like fmt and strings, need only their short name. However, your own and outside packages use the full path. Also, Go refuses to compile a file with an unused import, which keeps your code clean.
Adding Third-Party Go Modules
go get github.com/google/uuid
import "github.com/google/uuid"
id := uuid.New()
fmt.Println(id.String()) // a new random ID each run
To begin with, the go get command downloads the library and adds a require line to go.mod. It also creates go.sum, which stores checksums. In other words, a checksum is a fingerprint that proves nobody changed the code.
Useful Go Modules Commands
| Command | What it does |
|---|---|
| go mod init <path> | Starts a new module |
| go get <package> | Adds or updates a library |
| go mod tidy | Adds missing and removes unused libraries |
| go run . | Runs the main package in this folder |
| go build | Builds a program you can ship |
Therefore, run go mod tidy often, especially before you commit code. Consequently, it keeps go.mod and go.sum exactly in line with your imports.
Common Mistakes with Go Modules
| Error you see | Why it happens | How to fix it |
|---|---|---|
| go: go.mod file not found in current directory or any parent directory | The folder has no module yet. | Run go mod init first. |
| no required module provides package github.com/google/uuid | The library is not in go.mod. | Run go get or go mod tidy. |
| “os” imported and not used | An import is left over. | Delete it or use it. |
| undefined: greet.secret | The name starts with lowercase. | Capitalise it to export it. |
Indeed, the first error is very common when you open the wrong folder in your terminal. Module questions also come up in my Go interview questions and answers, so give them a try.
Official Resources
These pages come straight from the Go team, so they are always accurate and up to date.
Quick Recap
- A package is a folder of Go files, and a module is a set of packages.
- Go modules start with go mod init, which creates go.mod.
- Import your own packages with the module path plus the folder name.
- Capitalised names are exported, and lowercase names stay private.
- Use go get to add libraries and go mod tidy to clean up.
What’s Next?
Your project is now well organised. Next, we will start the most famous Go feature: running many tasks at once with goroutines.
Coming soon: Go Goroutines Explained for Beginners


