Go Mod: Cannot Find Module Providing Package
I Am Creating a Go Project with Version 1.12.1. If I Run Gopath="$(Pwd)/Vendor: $(Pwd)" Gobin="$(Pwd)/Bin" Go Clean I Get the Following Error: Can't Load...
I am creating a go project with version 1.12.1.
If I run GOPATH="$(pwd)/vendor:$(pwd)" GOBIN="$(pwd)/bin" go clean I get the following error:
can't load package: package unknown import path "": cannot find module providing package
This is only for go clean, go run or go build works fine.
Here is the folder structure of main code:
.
├── Makefile
├── cmd
│ └── server
│ └── main.go
├── go.mod
├── go.sum
└── pkg
└── storage
└── mysql
└── storage.go
Here is how the go.mod file looks like:
module
go 1.12
require (
v1.4.1
)
And finally the main.go file:
package main
import (
"fmt"
"os"
""
)
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "%v", err)
os.Exit(1)
}
}
func run() error {
// init storage
s := mysql.NewStorage()
// do some other stuff...
}
Any ideas what I am doing wrong?
3 Answers
I generally use go get and go mod tidy for same. It works all the time.
go mod tidy
Normally this new project approach works for me:
go mod init <project_name>
go test
I have found that developing projects outside of GOROOT and GOPATH are much easier
Go build/install is trying to find main package in your root directory, it is not checking sub-directories (cmd/server) in your case. Hence you are getting package not found error.
To properly build your code, you can run:
go build
Similarly, to run your project, you will have to provide module-name/main-package-path:
go run
Go clean can be executed in same way, by providing module-name/path-with-main-package
go clean
or
GOPATH="$(pwd)/vendor:$(pwd)" GOBIN="$(pwd)/bin" go clean
However, as per , just put your source files into your project’s root. It’s better that way.