> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Repositories Modules and Packages

> Explains Go repositories, modules, packages, module initialization with go.mod and dependency management for versioning, publishing, and organizing reusable Go libraries.

In this lesson we cover library management in Go and the three core concepts you need to master: repositories, modules, and packages. Understanding how these pieces fit together is essential for dependency management, versioning, and publishing reusable Go libraries.

A repository is the version-control location (for example, a Git repository) that holds one or more projects' source code. Repositories are familiar from tools like GitHub, GitLab, or Bitbucket and are the usual place you store your module(s).

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/NQ3INgb8I78TJYeC/images/Advanced-Golang/Introduction-Modules-Packages-and-Imports/Repositories-Modules-and-Packages/repository-database-server-rep-connection.jpg?fit=max&auto=format&n=NQ3INgb8I78TJYeC&q=85&s=2d839d09ac214254c5e1b53711686832" alt="A dark-themed slide titled &#x22;Repository&#x22; with a purple database icon labeled &#x22;server&#x22; connected by a dotted line to a server/rack icon labeled &#x22;rep.&#x22;" width="1920" height="1080" data-path="images/Advanced-Golang/Introduction-Modules-Packages-and-Imports/Repositories-Modules-and-Packages/repository-database-server-rep-connection.jpg" />
</Frame>

A module is a collection of Go packages that has a `go.mod` file at its root. The module is the unit of versioning and distribution in modern Go — it represents a library or an application. A module may contain one or more packages; packages are how Go organizes code into reusable units.

The module is identified by its module path (import path). For published modules, this should be a globally unique path (commonly the repository import path, e.g., `github.com/youruser/yourrepo`). For private modules you can use a private repository path or configure your tooling to resolve private hosts.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/NQ3INgb8I78TJYeC/images/Advanced-Golang/Introduction-Modules-Packages-and-Imports/Repositories-Modules-and-Packages/go-module-packages-kodekloud-learn-v1.jpg?fit=max&auto=format&n=NQ3INgb8I78TJYeC&q=85&s=225630137aa19d1244963a6e0ab9e91b" alt="A slide titled &#x22;Module & Packages&#x22; showing a Go module on the left branching to several packages. On the right it displays &#x22;Globally unique identifier&#x22; with the example github.com/kodekloud/learn and a v1.0.0 version tag." width="1920" height="1080" data-path="images/Advanced-Golang/Introduction-Modules-Packages-and-Imports/Repositories-Modules-and-Packages/go-module-packages-kodekloud-learn-v1.jpg" />
</Frame>

Quick summary of relationships and what `go.mod` records:

* Go code is grouped into packages.
* Packages are grouped into modules.
* Each module has a `go.mod` file that records the module path, the Go language version, and the other modules it depends on.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/NQ3INgb8I78TJYeC/images/Advanced-Golang/Introduction-Modules-Packages-and-Imports/Repositories-Modules-and-Packages/go-modules-packages-go-mod-file.jpg?fit=max&auto=format&n=NQ3INgb8I78TJYeC&q=85&s=d41318139ddff059bfd034d83a5c7312" alt="A dark-themed slide titled &#x22;Module & Packages&#x22; that explains Go concepts with two boxed points: one says Go code is grouped into packages and packages into modules, and the other explains a module lists dependencies and Go version in the go.mod file. Each point is accompanied by a colored check icon." width="1920" height="1080" data-path="images/Advanced-Golang/Introduction-Modules-Packages-and-Imports/Repositories-Modules-and-Packages/go-modules-packages-go-mod-file.jpg" />
</Frame>

Definitions at a glance

| Resource | Role | Typical identifier / example |
| - | - | - |
| Repository | Version-controlled storage for source code | `github.com/youruser/yourrepo` |
| Module | Versioned collection of packages with `go.mod` | `module example.com/learn` |
| Package | Namespace for Go source files (import path used within code) | `example.com/learn/pkg/mathutil` |

Creating a module — step by step

1. Pick or create a directory where you keep your project source. In this example we create `learn` under a local workspace.
2. Run `go mod init` with the module path. The module path should be globally unique when you intend to publish the module; otherwise use a stable private import path.

Example commands:

```bash theme={null}
# from the directory where you want your project
mkdir -p ~/Desktop/kodekloud/learn
cd ~/Desktop/kodekloud/learn

# initialize the module with a globally unique path
go mod init example.com/learn
```

Expected output:

```bash theme={null}
$ go mod init example.com/learn
go: creating new go.mod: module example.com/learn
```

This will create a `go.mod` file in the `learn` directory. Initially it contains the module path and the Go version. As you add imports from other modules, `go` commands will update `go.mod` and create a `go.sum` file to lock dependency checksums.

<Callout icon="lightbulb" color="#1CB2FE">
  Choose a stable, unique module path for the life of the module. If you plan to publish, use the canonical repository import path (for example, `github.com/youruser/yourrepo`). For private modules you can manage local overrides with the `replace` directive in `go.mod` or configure private proxy settings.
</Callout>

We will examine the `go.mod` file format, dependency versioning, and common workflows (like `go get`, `go list -m all`, and `go mod tidy`) in later lessons.

Links and references

* [Go Modules: official documentation](https://go.dev/ref/mod)
* [Managing Go modules](https://go.dev/doc/modules/managing-dependencies)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/advanced-golang/module/6ce7d245-515e-428d-9672-915e49a8ebd6/lesson/cd2269c6-f41c-45c1-aee2-308df54bcd46" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.