In this tutorial, we will learn about modules.
What are modules?
Simply defined, A module is a collection of Go packages stored in a file tree with a go.mod
file at its root, provided the directory is outside $GOPATH/src
.
Go modules were introduced in Go 1.11, which brings native support for versions and modules. Earlier, we needed the GO111MODULE=on
flag to turn on the modules functionality when it was experimental. But now after Go 1.13 modules mode is the default for all development.
But wait what is GOPATH
?
Well, GOPATH
is a variable that defines the root of your workspace and it contains the following folders:
- src: contains Go source code organized in a hierarchy.
- pkg: contains compiled package code.
- bin: contains compiled binaries and executables.
Like earlier, let's create a new module using go mod init
command which creates a new module and initializes the go.mod
file that describes it.
$ go mod init example
The important thing to note here is that a Go module can correspond to a Github repository as well if you plan to publish this module. For example:
$ go mod init example
Now, let's explore go.mod
which is the file that defines the module's module path and also the import path used for the root directory, and its dependency requirements.
module <name>
go <version>
require (
...
)
And if we want to add a new dependency, we will use go install
command:
$ go install github.com/rs/zerolog
As we can see a go.sum
file was also created. This file contains the expected hashes of the content of the new modules.
We can list all the dependencies using go list
command as follows:
$ go list -m all
If the dependency is not used, we can simply remove it using go mod tidy
command:
$ go mod tidy
Finishing up our discussion on modules, let's also discuss vendoring.
Vendoring is the act of making your own copy of the 3rd party packages your project is using. Those copies are traditionally placed inside each project and then saved in the project repository.
This can be done through go mod vendor
command.
So let's reinstall the removed module using go mod tidy
.
package main
import "github.com/rs/zerolog/log"
func main() {
log.Info().Msg("Hello")
}
$ go mod tidy
go: finding module for package github.com/rs/zerolog/log
go: found github.com/rs/zerolog/log in github.com/rs/zerolog v1.26.1
$ go mod vendor
├── go.mod
├── go.sum
├── go.work
├── main.go
└── vendor
├── github.com
│ └── rs
│ └── zerolog
│ └── ...
└── modules.txt
This article is part of my open source Go Course available on Github.
karanpratapsingh / learn-go
Master the fundamentals and advanced features of the Go programming language
Learn Go
Hey, welcome to the course, and thanks for learning Go. I hope this course provides a great learning experience.
This course is also available on my website and as an ebook on leanpub. Please leave a ⭐ as motivation if this was helpful!
Table of contents
-
Getting Started
-
Chapter I
-
Chapter II
-
Chapter III
-
Chapter IV
-
Appendix
What is Go?
Go (also known as Golang) is a programming language developed at Google in 2007 and open-sourced in 2009.
It focuses on simplicity, reliability, and efficiency. It was designed to combine the efficacy, speed…
Top comments (0)