The below error occurred when I had changed GOPATH, made two packages, and written some codes.
% package other/pkg1 is not in GOROOT (/usr/local/go/src/other/pkg1)
The cause is a package structure. Putting it together solves it.
Error
Below is the directory structure when the error occurred.
${GOPATH}/src
|-- other
|   |-- go.mod
|   `-- pkg1
|       `-- pkg1.go
`-- prj
    |-- go.mod
    `-- main.go
prj package is unable to refer to other/pkg1 although it is in GOPATH.
Codes are below.
prj/main.go
package main
import "other/pkg1"
func main()  {
    pkg1.Func()
}
other/pkg1/pkg1.go
package pkg1
import "fmt"
func Func() {
    fmt.Println("called func in pkg1")
}
Fix
To include other in prj solves the error.
${GOPATH}/src
`-- prj
    |-- go.mod
    |-- main.go
    `-- other
        `-- pkg1
            `-- pkg1.go
prj/main.go
package main
import "prj/other/pkg1"
func main()  {
    pkg1.Func()
}
 

 
    
Top comments (3)
i have the same problem. but i can't fix it.
error: main.go:5:2: package lessons/lesson1/util is not in GOROOT (C:\Program Files\Go\src\lessons\lesson1\util)
proj: dev-to-uploads.s3.amazonaws.com/up...
Thank you for your feedback. I don't know the matter why the error happens right now. I'll check that.πͺ
I met a similar problem.
When the issue happened, my folder structure looked like this:
I got an error message that says:
After some experimenting, I realized what I did wrong was
go mod initat/internal/dbconn. I only needed to initialize a module atdbmock/.By removing
internal/dbconn/go.mod(ΒΆ) and only keepingdbmock/go.mod(Β§), it is working fine now.For your reference,
importsection indbmock.golooks like this now: