Project Setup and Enabling Modules
Before we write any code, you’ll need to first set up a project directory (also known as a repository) on your computer which follows the Go conventions.
This directory will act as the top-level ‘home’ for our Snippetbox project throughout this book — all the Go code we write will live in there, along with other project-specific assets like HTML templates and CSS files.
So where should this project directory live, and what should we call it?
Open a terminal window and run the
go env command to get the information about your current Go installation and environment. The output should look something like this:
The important thing to look at here is the
GOPATH value — which in my case is the directory
For older versions of Go (pre-1.11) it was best practice to locate your project repositories within a
src folder under your
But if you’re running Go 1.11 and using modules to manage your dependencies — like we will be in this book — then this rule no longer applies. You can create your project repository anywhere on your computer, and it’s actually simplest if you locate it outside of your
So, if you’re following along, open your terminal and run the following commands to create a new project directory called
snippetbox. I’m going to locate this under
$HOME/code, but you can choose a different location if you wish.
While we’re at it, let’s also add an empty
main.go file to the project directory:
The next thing we need to do is let Go know that we want to use the new modules functionality to help manage (and version control) any third-party packages that our project imports.
To do this we first need to decide what the module path for our project should be.
The important thing here is uniqueness. To avoid potential import conflicts with other people’s packages or the standard library in the future, you want to choose a module path that is globally unique and unlikely to be used by anything else. In the Go community, a common convention is to namespace your module paths by basing them on a URL that you own.
In my case a clear, succinct and unlikely-to-be-used-by-anything-else module path for this project would be
alexedwards.net/snippetbox, and we’ll use this throughout the rest of the book. If possible, you should swap this for something that’s unique to you instead.
Now that we’ve decided a unique module path let’s enable modules for the project.
To do this make sure that you’re in the root of your project directory, and then run the
go mod init command — passing in your module path as a parameter like so:
At this point your project directory should look a bit like the screenshot below. Notice the
go.mod file which has been created?
At the moment there’s not much going on in this file, and if you open it up in your text editor it should look like this (but preferably with your own unique module path instead):
Later in our build we’ll see how this file is used to define the third-party packages (and their versions) which are required by our project.
Module Paths for Downloadable Packages
If you’re creating a package or application which can be downloaded and used by other people and programs, then it’s good practice for your module path to equal the location that the code can be downloaded from.
For instance, if your package is hosted at
https://github.com/foo/bar then the module path for the project should be