agriz
agriz

Reputation: 759

Package is not in GOROOT

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?

Upvotes: 18

Views: 58284

Answers (2)

agriz
agriz

Reputation: 759

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.

Upvotes: 20

Shailesh Suryawanshi
Shailesh Suryawanshi

Reputation: 1270

You can use go mod and explicitly give the path to the module you want to initialize.

From Documentation

Init initializes and writes a new go.mod to the current directory, in effect creating a new module rooted at the current directory. The file go.mod must not already exist. If possible, init will guess the module path from import comments (see 'go help importpath') or from version control configuration. To override this guess, supply the module path as an argument.

Example

   'go mod init example.com/m' to initialize a v0 or v1 module
   'go mod init example.com/m/v2' to initialize a v2 module

Further details on how to setup your go-program can be found here.

Upvotes: 6

Related Questions