Golang is weird!

I have just managed to get Golang to use local packages.

Go takes the approach that all packages are to be considered to be remote even if they are not.
This means that you need to define your projects package location in go.mod as if it were installed from the remote repository that you will deploy it to. You can then import the local module as if it were from that location but it will resolve to a local copy.

package github.com/company-name/project-name/project-root

You can then import github.com/company-name/project-name/project-root/mymodule

from a ./mymodule folder in your project. All go files in that folder will be imported.

The practical advantage is that you can use the same sample code if the library is local or remote without needing a distinct bundling tool.

The go test command is also odd. It by default only runs the tests in the current folder.
You need to use go test. ./... to get all the tests to run (which will be all files named *_test.go in the filesystem.

This is not very intuitive.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s