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

# gomod

> Explains Go modules and the go.mod file, how to initialize modules, manage dependencies with go mod tidy and go get, and keep go.mod and go.sum in sync.

In this lesson you'll learn what a `go.mod` file is, how to create one, and how to manage dependencies with Go modules.

A directory becomes a Go module when it contains a valid `go.mod` file in its root. Use `go mod init` to create this file and record the module path.

Example: initialize a module

```bash theme={null}
go mod init example.com/learn
```

A minimal `go.mod` looks like:

```text theme={null}
module example.com/learn

go 1.19
```

* The file starts with a module declaration (`module <module-path>`).
* It commonly includes the minimum supported Go version (here `go 1.19`).

<Callout icon="warning" color="#FF6B6B">
  Make sure the module path you choose is unique and, if you plan to publish the module, matches the repository location (for example `github.com/<org>/<repo>`). Changing the module path later can be disruptive.
</Callout>

Create `main.go` that imports a third-party package and uses it. Note the exported function name `NewRandom()` (capital R):

```go theme={null}
package main

import (
	"fmt"

	"github.com/pborman/uuid"
)

func main() {
	u := uuid.NewRandom()
	fmt.Println(u)
}
```

At this point the code imports a third-party package but you haven't downloaded it yet. Use:

```bash theme={null}
go mod tidy
```

What `go mod tidy` does

* Adds missing module requirements needed to build the module.
* Removes unused requirements.
* Updates `go.sum` to match the resolved dependency graph.
  This keeps `go.mod` and `go.sum` in sync with your source code.

Representative output from `go mod tidy`:

```bash theme={null}
go: finding module for package github.com/pborman/uuid
go: found github.com/pborman/uuid in github.com/pborman/uuid v1.2.1
```

After running `go mod tidy`, your `go.mod` will include resolved dependencies. Example:

```text theme={null}
module example.com/learn

go 1.19

require github.com/pborman/uuid v1.2.1

require github.com/google/uuid v1.0.0 // indirect
```

Run the program:

```bash theme={null}
go run main.go
```

Example runtime output:

```bash theme={null}
0fc766ac-80d1-4999-9ab4-701d50600cb8
```

Quick reference — common Go module commands

| Command | Purpose | Example output / note |
| -: | - | - |
| `go mod init <module-path>` | Create `go.mod` and set module path | `go mod init example.com/learn` |
| `go mod tidy` | Add missing deps and remove unused ones | Updates `go.mod` and `go.sum` |
| `go get <module>@<version>` | Add or upgrade a dependency | `go get github.com/pborman/uuid@v1.2.1` |
| `go mod download` | Download modules without building | Useful in CI |

Links and references

* [Go Modules overview](https://go.dev/doc/modules)
* `github.com/pborman/uuid` — package used in this example: [https://github.com/pborman/uuid](https://github.com/pborman/uuid)

Summary

* A directory is a Go module when it contains a valid `go.mod`.
* `go.mod` contains the module declaration, the minimum Go version, and required module dependencies.
* Use `go mod tidy` frequently to keep `go.mod` and `go.sum` consistent with your code.
* The `replace` directive can be used in `go.mod` to override module versions or point to local copies during development.

<Callout icon="lightbulb" color="#1CB2FE">
  Use `go mod tidy` frequently during development to ensure `go.mod` and `go.sum` stay in sync with your source code.
</Callout>

<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/1938db03-ea4b-47ac-8ab3-6238aaf4bc56" />
</CardGroup>


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