Package is not in goroot

When you encounter the error message “package is not in goroot”, it means that the package you are trying to import is not located in the Go root directory. The Go root directory is the installation directory where the Go programming language is installed on your system.

By default, the Go root is usually set to the directory where Go is installed, such as /usr/local/go on Unix-based systems. However, you can also set a custom Go root using the GOROOT environment variable.

To fix the “package is not in goroot” error, you need to make sure that the package you are trying to import is installed within the Go root directory or in the GOPATH directory.

Example

Let’s say you have a project with the following directory structure:

  ├── main.go
  └── utils
      └── helper.go
  

In your main.go file, you want to import the helper package from the utils directory. To do that, you should set the Go module properly and import the package as follows:

  package main
  
  import (
  	"fmt"
  	"your/go/project/utils"
  )
  
  func main() {
  	utils.HelperFunction()
  }
  

However, if you mistakenly specify the incorrect package path or the package is not located in the Go root or GOPATH, you may encounter the “package is not in goroot” error message.

To resolve the error, you can either move the utils directory to a location within the Go root or GOPATH, or install the package globally using the go install command:

  go install your/go/project/utils
  

After installing the package, you should be able to import it without encountering the “package is not in goroot” error.

Read more interesting post

Leave a comment