Minimalist Guide for Go Modules with Sub-Folders and Monorepos

Original Go Directory Structure

When Go first came out, the default structure (on mac or linux) was to put all golang code into ~/go/src with a directory structure matching the import path. For example, a Go package import would be at ~/go/src/

A sub-directory would be imported similarly, for example a package in the github repo foo/bar under a folder stuff/ would be imported using import and live in the file system at ~/go/src/

Go modules allowed changing this semi-forced directory structure.

Go modules no longer require code to be at ~/go/src/.

Directory Structure for Monorepo-Style Go projects

It is still possible to use the original ~/go/src/ format, but no longer necessary.

Suppose you have a repository outside ~/go/src/, instead at ~/myproj/.

Suppose the directory ~/myproj/ is a repository at

Inside it, you have several golang packages:


What would the imports look like? How would the go modules be setup?

One option is to initialize the ~/myproj/gocode of the folder with:
go mod init which creates the file go.mod with the contents:

go 1.15

Now if you open up ~/myproj/gocode/app/main.go you can import the other services as follows:

import (

Note that the import path is relative to the go.mod file.

Final Thoughts

The above example is not necessarily a desired way to setup a package. It is an illustration of go mod flexibility.