go build not looking in $GOPATH

1.1k views Asked by At

I have set $GOPATH, and importing some part of my source code which is present in $GOPATH/src.

Package I need to import is written by me and stored in GOPATH/src I have named it as otelkafkago and it's path is GOPATH/src/otelkafkago

I have one more code base elsewhere on my HardDisk and when I try to compile that, it gives me

main.go:51:2: package otelkafkago is not in GOROOT (/usr/local/go/src/otelkafkago)

I have imported it as

import(
    "otelkafkago"
)

Following is my screen capture during build,

kshitijpatil@PNQ-KPATIL checkoutservice % echo $GOPATH
/Users/kshitijpatil/go/
kshitijpatil@PNQ-KPATIL checkoutservice % go build
main.go:51:2: package otelkafkago is not in GOROOT (/usr/local/go/src/otelkafkago)
kshitijpatil@PNQ-KPATIL checkoutservice % env GOPATH=/Users/kshitijpatil/go go build
main.go:51:2: package otelkafkago is not in GOROOT (/usr/local/go/src/otelkafkago)
kshitijpatil@PNQ-KPATIL checkoutservice % 
1

There are 1 answers

4
VonC On BEST ANSWER

Since Go 1.16, the module-aware mode is enabled by default, regardless of whether a go.mod file is present in the current working directory or a parent directory.

More precisely, the GO111MODULE environment variable now defaults to on. To switch to the previous behavior, set GO111MODULE to auto.

In your case, it would be easier to migrate to go modules, even though setting GO111MODULE to auto might help.

As commented, the first line of your go.mod should be:

module myProject

Your other "otelkafkago" project, it should have its own go.mod with module otelkafkago as first line, and be built first.