Home >Backend Development >Golang >Go Module Error: Why is 'package package1 is not in GOROOT' and how do I fix it?
Go Module Error: Package Not in GOROOT
Issue:
When running go run main.go, users encounter the following error:
package package1 is not in GOROOT (/usr/local/go/src/package1)
Cause:
This error occurs when the Go module system cannot locate the specified package in the GOROOT directory.
Solution:
To resolve this issue, ensure that the environment variables are properly configured. The following steps should be followed:
Configure GOPATH:
Set the GOPATH environment variable to point to this directory:
export GOPATH=/mnt/sda1/programming/gopath
Adjust GOROOT:
Ensure that GOROOT is set to the system-installed Go root directory:
export GOROOT=/usr/local/go
Add GOROOT to PATH:
Add GOROOT to the PATH environment variable:
export PATH=$PATH:$GOROOT/bin
Enable Go Modules:
Set the GO111MODULE environment variable to on:
export GO111MODULE=on
Update Bashrc:
Load the .bashrc file in the terminal:
source ~/.bashrc
Reinitialize Module:
Create Package Directory:
Import Package:
In main.go, import the package:
import "main/package1"
Load bashrc again:
Once these steps are complete, the package package1 is not in GOROOT error should be resolved, and the main.go file should run successfully.
The above is the detailed content of Go Module Error: Why is 'package package1 is not in GOROOT' and how do I fix it?. For more information, please follow other related articles on the PHP Chinese website!