Question: Encountering the error "package package1 is not in GOROOT (/usr/local/go/src/package1)" when trying to use an external package in Go.
Explanation: This error occurs when the Go compiler cannot find the specified package in the standard library GOROOT directory. It suggests that the package is not installed or is located outside the standard library.
Solution:
Configure Environment Variables:
For example, you can add these lines to your .bashrc file:
export GO111MODULE=on export GOPATH=/mnt/sda1/programming/gopath export PATH=$PATH:$GOPATH/bin export GOROOT=/usr/local/go export PATH=$PATH:$GOROOT/bin
Load the Environment Variables:
Initialize Main Package:
Create External Package:
Import External Package:
import "main/package1"
By following these steps, you can ensure that your Go compiler can locate and use the external package, resolving the "package package1 is not in GOROOT" error.
The above is the detailed content of Why Can't My Go Compiler Find My External Packages?. For more information, please follow other related articles on the PHP Chinese website!