go.mod — What, Why and How?

Hardik Mehta
3 min readDec 4, 2024

--

Photo by Jamie Taylor on Unsplash

Let me break it down for you — the what, why, and how of go.mod. Hopefully, by the end of this post, you’ll probably appreciate it as much as I do.

What Is It?

Think of go.mod as your Go project’s blueprint. It’s where your project declares:

  1. Its Name: Like a title for your app or library.
  2. Dependencies: All the external libraries it relies on, and their versions.
  3. Go Version: The Go version it’s written for.

Here’s what a go.mod file might look like for a basic project:

module github.com/yourusername/myappgo 1.21require (
github.com/gin-gonic/gin v1.8.1
github.com/sirupsen/logrus v1.9.0
)

Why Do We Need It?

Before go.mod, Go used the infamous GOPATH. If you didn’t live through those days, count yourself lucky. Everything had to sit inside a specific directory (GOPATH), and dependency management? Forget about it. You were on your own.

Here’s why go.mod was a game-changer:

  1. No More GOPATH Prison: Your projects can live anywhere—on your desktop, in a random folder, or even on the moon (okay, not literally).
  2. Versioning Made Simple: Want version 1.2.3 of a library? go.mod makes sure you get it. Want to update? It’s a breeze.
  3. Reproducibility: Collaborators can clone your project and run it with zero guesswork about dependencies.

For me, the biggest win was how it made Go projects feel modular and portable. No more dependency burden.

How To Use It?

Using go.mod is surprisingly straightforward. Here’s a quick example:

1. Start a New Module

Navigate to your project folder and run:

go mod init github.com/yourusername/myapp

This creates a go.mod file:

module github.com/yourusername/myapp
go 1.21

Tip: If you’re working on something local or private, you can use a simple name like myapp instead of a GitHub-style path.

2. Add Dependencies

When you import a package in your code, Go automatically updates go.mod when you build or run the project. But you can also add dependencies manually:

go get github.com/gin-gonic/gin@v1.8.1

This adds the dependency to go.mod:

require github.com/gin-gonic/gin v1.8.1

3. Clean Up with go mod tidy

If you’ve been experimenting and added some unnecessary dependencies, run:

go mod tidy

It’ll remove unused imports and make everything neat again. It’s like Marie Kondo for your project.

Common Pitfalls with go.mod

Even with its simplicity, go.mod can trip you up. Here are a few common issues I’ve run into (and how to fix them):

Problem: “Current file is not included in a workspace module”

This happens when you try to run a Go file that isn’t part of a module. The fix? Run:

go mod init myapp

Problem: “Unwanted dependencies”

Maybe you added a library you no longer need. Just tidy things up:

go mod tidy

Problem: “Dependency conflicts”

If two dependencies rely on different versions of the same library, Go’s module system resolves it for you. You can also force specific versions using:

go get dependency@version

Quick Recap

  • go.mod is your project’s blueprint for managing dependencies and versioning.
  • It replaces the old GOPATH system, making Go projects more modular and portable.
  • You can set it up in seconds using go mod init.
  • It’s easy to clean, update, and debug with commands like go mod tidy and go get.

--

--

Hardik Mehta
Hardik Mehta

Written by Hardik Mehta

Believer that everything testes better with butter. https://hardikm9850.github.io/

No responses yet