Error "Package Not in GOROOT": Resolving the Issue
When running go run main.go, you may encounter the error, "package package1 is not in GOROOT (/usr/local/go/src/package1)" if the environment variables are improperly configured. To resolve this issue, meticulously follow these steps:
Configure Environment Variables:
In the bashrc file, set the following environment variables:
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 bashrc File:
Load the bashrc file into the terminal using the following command:
source ~/.bashrc
Create Project Structure:
Create a main folder and place main.go inside it. Initialize the project with the following command:
go mod init main
Create a separate folder for the package (e.g., package1) and place your Go files in it, but do not create a go.mod file in this folder.
Import Package in Main:
In your main.go, import the package using the following syntax:
import "main/package1" y := package1.Struct1{a: 1, b: 2,...} z := y.func1()
The above is the detailed content of Why Am I Getting the 'Package Not in GOROOT' Error in Go, and How Do I Fix It?. For more information, please follow other related articles on the PHP Chinese website!