Go Notes

Modules

References

  1. Go modules wiki
  2. Introduction blog post

Quick-start

To get started on a throw-away project (that will never be published &c.):

go mod init temp

At that point, go get, go build and the rest will be “module aware”, and you can play around without needing to worry about go-path and the global go-workspace.

For a real project:

# Pick your source-host here
go mod init gitlab.com/group/project

is enough.

Multiple packages per repo

If you’re maintaining multiple Go packages in a single Git repo, the typical approach is to create a module at the repo-root. The Go tooling is smart-enough to use the local version of all packages within the module (even if you’re using a fully-qualified import).

Upgrading dependencies

I’m bad at this, at the tooling feels un-natural every time I do it. So - read the references.

When all else fails, you can delete all of the require statements from your go.mod, delete your go.sum file, re-do go mod tidy and see what’s different.

Testing

Tests for the file foo.go should live in foo_test.go.

Example:

package yourPackage

import "testing"

func TestAThing(t *testing.T) {
    t.Errorf("A failure!")
}

Run the tests with go test.