ubuntugogopath

golang compile simple app module not found (GOPATH?)


just started with go, got stuck on a very simple application (from here https://golang.org/doc/tutorial/call-module-code) when I try to compile the hello.go:

...
hello.go:6:5: cannot find module providing package example.com/greeting
...

The env is set like this (Ubuntu 20.04)

root@591c0587972e:~/proj/test/go.test# go env | grep GOPA
GOPATH="/root/go:/root/proj/test/go.test"

root@591c0587972e:~/proj/test/go.test# go version
go version go1.15.3 linux/amd64

the source code https://github.com/myxit/golang-wtf

Please help, what is the problem with setup?


Solution

  • You'll need to use a replace directive to point to the local path to the package. Otherwise Go tries to find it where the actual path is -- at example.com/greeting. In the page you link to, this is mentioned:


    For production use, you’d publish your modules on a server, either inside your company or on the internet, and the Go command will download them from there. For now, you need to adapt the caller's module so it can find the greetings code on your local file system.

    To do that, make a small change to hello module’s go.mod file.

    In the hello directory, open the go.mod file, change it so that it looks like the following, and save the file.

    module hello
    
    go 1.14
    
    replace example.com/greetings => ../greetings
    

    In fact, since you're using modules (as you should with go 1.15!) I would recommend not setting at all. It's not needed, and just adds confusion in "module mode".