Switch over to go mod causing build errors

I just followed the steps to convert my project to a using modules with go mod init and go mod tidy and now I get these not found errors. Details as follows:

>env|grep -i go
OLDPWD=/home/reg/Jobs/Go/src/myproject
_INTELLIJ_FORCE_SET_GOROOT=/usr/lib64/go/1.14
GOPATH=/home/reg/Jobs/Go
PWD=/home/reg/Jobs/Go/src/myproject/server
GOROOT=/usr/lib64/go/1.14
_INTELLIJ_FORCE_PREPEND_PATH=/usr/lib64/go/1.14/bin:/home/reg/Jobs/Go/bin:
PATH=/usr/lib64/go/1.14/bin:/home/reg/Jobs/Go/bin:/home/reg/bin:/usr/local/bin:/usr/bin:/bin:/usr/lib/mit/bin:/usr/lib/mit/sbin:/usr/lib64/go/1.14/bin:/home/reg/Jobs/Go/bin
_INTELLIJ_FORCE_SET_GOPATH=/home/reg/Jobs/Go

And for example where my main.go file worked perfectly well with imports like:

import (
	"fmt"
	"os"
	"strconv"
	"time"

	"github.com/davecgh/go-spew/spew"

	`myproject/server/modules/core/boot`
	`myproject/...`
	`myproject/...`
	`myproject/...`
)

Now all the imports that refer to my submodules, that is starting with myproject/, are generating errors like:

main.go:25:2: package myproject/server/modules/core/boot is not in GOROOT (/usr/lib64/go/1.14/src/myproject/server/modules/core/boot)

And go.mod looks like:

module github.com/mycompany/myproject

go 1.14

require (
	github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf
	...
	...
	...
)

I’m sure there is a simple fix for this but I have never used go.mod so I’m hoping someone can tell me what’s required at this point.

It’s never simple to migrate non-module to module, I’m afraid. :upside_down_face: You do to sort this out for each mis-aligned dependencies.

You’re looking for replace directive and redirect all your myProject to local dependency pathing. The is caused by your import statement. Assuming that myProject is your project root repository, the clause should be something like:

replace (
         "myproject" => ./
)

General Practice

When using go.mod, you use import using the public pathing + replace clause, as in:

import "github.com/<name>/<project>/<path>/<to>/<package>"

Then in go.mod, replace your remote source with local directory, as in:

replace (
         "github.com/<name>/<project>" => ./
)

That way, go module will look into local directory first before pulling a remote version. On user side, it is aligned with effective Go and never gets confused with import statement.

1 Like

Thank you!

With this advice and a lot reading tonight I managed to get my project working in module mode.

For anyone else who has to do this I had to do three things:

  1. I read the entire page you linked replace directive
  2. Moved my project root to align it naturally with git root
  3. Did go mod init, go mod tidy
  4. Did a global search and replace in my IDE with:
    a) Search Regex: [`"](myFirstPathSegment/.*)[`"]
    b) Replace: `github.com/myproject/$1`

And it worked. With those changed I didn’t even need the replace command in the go.mod file.

Ideally yes. However, if you are doing local devlopement (e.g. adding feature), you gonna need the local direcotry clause to prevent the compiler from keep pulling from remote every time you run a test or build instead of against your local code changes.

I do not know why this is not built in but I hope in near future.

1 Like

Good advice, thanks.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.