Package is not in GOROOT

Issue

main.go:5:2: package greetings is not in GOROOT (C:\Program Files\Go\src\greetings)

I am reading Head first go and it was written to create folder in c:/users/username/go/src/packagename and c:/users/username/go/src/project_name

when i try to run

go run main.go from project_name folder, i got the following error.
main.go:5:2: package greetings is not in GOROOT (C:\Program Files\Go\src\greetings)

C:\Users\agriz>go env
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\agriz\AppData\Local\go-build
set GOENV=C:\Users\agriz\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\agriz\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\agriz\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.16.7
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\agriz\AppData\Local\Temp\go-build2047492330=/tmp/go-build -gno-record-gcc-switches

I dont want to keep the code in c drive. I want to run codes from D:/ is that possible? How can i do that?

Solution

set GO111MODULE=off and set GOPATH to desired location is also working good.

go env -w GO111MODULE=off

and

set GOPATH=D:\go

This one fixed my problems.

Answered By – agriz

Answer Checked By – Senaida (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.