DEV Community

loading...

Go - package is not in GOROOT

#go
takakd profile image Takahiro Kudo ・1 min read

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)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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()
}
Enter fullscreen mode Exit fullscreen mode

other/pkg1/pkg1.go

package pkg1

import "fmt"

func Func() {
    fmt.Println("called func in pkg1")
}
Enter fullscreen mode Exit fullscreen mode

Fix

To include other in prj solves the error.

${GOPATH}/src
`-- prj
    |-- go.mod
    |-- main.go
    `-- other
        `-- pkg1
            `-- pkg1.go
Enter fullscreen mode Exit fullscreen mode

prj/main.go

package main

import "prj/other/pkg1"

func main()  {
    pkg1.Func()
}
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

pic
Editor guide