You're reading a sample of this book. Get the full version here.
Let's Go Foundations › Project Setup and Enabling Modules
Previous · Contents · Next
Chapter 2.2.

Project Setup and Enabling Modules

Before we write any code, you’ll need to create a snippetbox directory on your computer to act as the top-level ‘home’ for this project. All the Go code we write throughout the book will live in here, along with other project-specific assets like HTML templates and CSS files.

So, if you’re following along, open your terminal and create a new project directory called snippetbox anywhere on your computer. I’m going to locate my project directory under $HOME/code, but you can choose a different location if you wish.

$ mkdir -p $HOME/code/snippetbox

While we’re at it, let’s also add an empty main.go file to the project directory:

$ cd $HOME/code/snippetbox
$ touch main.go

Enabling Modules

The next thing we need to do is let Go know that we want to use modules functionality to help manage (and version control) any third-party packages that our project imports.

But before we can do that, we need to decide what the module path for our project should be.

If you’re not already familiar with Go’s module functionality, the module path is essentially just the canonical name or identifier for your project. Although you can use (almost) anything as the module path, the important thing is uniqueness. To avoid potential import conflicts with other people’s packages or the standard library in the future, you want to pick 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:

$ cd $HOME/code/snippetbox
$ go mod init alexedwards.net/snippetbox
go: creating new go.mod: module alexedwards.net/snippetbox

At this point your project directory should look a bit like the screenshot below. Notice the go.mod file which has been created?

02.02-01.png

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):

File: go.mod
module alexedwards.net/snippetbox

go 1.15

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.

There’s now just one more thing we need to check. 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:

$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/alex/.cache/go-build"
GOENV="/home/alex/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/alex/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/alex/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/alex/Work/Golang College/Code/golangcollege.com/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build733636700=/tmp/go-build -gno-record-gcc-switches"

The important thing to look at here is the value of the GO111MODULE environment variable — which in my case is the empty string "". In order for Go to use modules, this must have the value On, Auto or the empty string "". If it is set to Off, please change this environment variable before continuing. If you’re interested in understanding exactly what these different values mean, then there is more information here.

Additional Information

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 github.com/foo/bar.