The issue arises when attempting to build a Go project, and the following error appears: "package project/game is not in GOROOT (C:Gosrcprojectgame)".
This error typically occurs when:
1. Configure Environment Variables
If you upgraded to a newer Go version (1.13 ), environment variables like GOROOT, GOBIN, and GOPATH are no longer recommended.
2. Correct Project Structure
Ensure that your project has a go.mod file at the project root and the following directory structure:
|- project |- game |- entity |- game_stuff.go |- server
3. Use go mod
Instead of rely on environment variables, use Go Modules (go mod) to manage module dependencies.
cd project go mod init remote-repo.com/username/repository
4. Run Commands from the Module Root
Commands should be executed from the project root directory. For example:
go run server
5. Specify Full Module Paths
When using go commands, especially from outside the module root, specify the full package path, which includes the vendor URI. For example:
go test github.com/username/repository/project/game/entity
6. Set GOPATH If Necessary (Optional)
For older Go versions, you can optionally set GOPATH to the workspace path where your project is located. Ensure that GOPATH points to the correct path.
GOPATH=C:\Users\username\go
Example
To build the server package in the provided directory structure:
cd project/server go build project/server
This command should successfully build the server package without the "package XXX is not in GOROOT" error.
The above is the detailed content of Why Am I Getting the 'package XXX is not in GOROOT' Error When Building My Go Project?. For more information, please follow other related articles on the PHP Chinese website!