Go - package is not in GOROOT
Takahiro Kudo
Posted on March 2, 2021
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()
}
💖 💪 🙅 🚩
Takahiro Kudo
Posted on March 2, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
githubcopilot AI Innovations at Microsoft Ignite 2024 What You Need to Know (Part 2)
November 29, 2024