go module
urgensherpa
Posted on February 4, 2024
Go programs are organized into packages. A package is a directory of Go code that's all compiled together. Functions, types, variables, and constants defined in one source file are visible to all other source files within the same package (directory).
A repository contains one or more modules. A module is a collection of Go packages that are released together.
A GO REPOSITORY TYPICALLY CONTAINS ONLY ONE MODULE, LOCATED AT THE ROOT OF THE REPOSITORY.
A file named go.mod at the root of a project declares the module. It contains:
The module path
The version of the Go language your project requires
Optionally, any external package dependencies your project has
The module path is just the import path prefix for all packages within the module. Here's an example of a go.mod file:
module github.com/user123/exampleproject
go 1.20
require github.com/google/examplepackage v1.3.0
Each module's path not only serves as an import path prefix for the packages within but also indicates where the go command should look to download it. For example, to download the module golang.org/x/tools, the go command would consult the repository located at https://golang.org/x/tools.
An "import path" is a string used to import a package. A package's import path is its module path joined with its subdirectory within the module. For example, the module `github.com/google/go-cmp` contains a package in the directory cmp/. That package's import path is github.com/google/go-cmp/cmp.
Packages in the standard library do not have a module path prefix.
DO I NEED TO PUT MY PACKAGE ON GITHUB?
You don't need to publish your code to a remote repository before you can build it. A module can be defined locally without belonging to a repository. However, it's a good habit to keep a copy of all your projects on a remote server, like GitHub.
The $GOPATH environment variable will be set by default somewhere on your machine (typically in the home directory, ~/go). Since we will be working in the new "Go modules" setup, you don't need to worry about that. If you read something online about setting up your GOPATH, that documentation is probably out of date.
These days you should avoid working in the $GOPATH/src directory. Again, that's the old way of doing things and can cause unexpected issues, so better to just avoid it.
GET INTO YOUR WORKSPACE
Navigate to a location on your machine where you want to store some code. For example, I store all my code in ~/workspace, then organize it into subfolders based on the remote location. For example,
~/workspace/github.com/Stebalien/go-address-validator = https://github.com/Stebalien/go-address-validator
That said, you can put your code wherever you want.
FIRST LOCAL PROGRAM
Create a new directory and enter it:
mkdir hellogo
cd hellogo
Inside the directory declare your module's name:
go mod init {REMOTE}/{USERNAME}/hellogo
Where {REMOTE} is your preferred remote source provider (i.e. github.com) and {USERNAME} is your Git username. If you don't use a remote provider yet, just use example.com/username/hellogo
Print your go.mod file:
cat go.mod
Why does Go include a remote URL in module paths?
- to simplify remote download of pkgs
Posted on February 4, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.