go mod: cannot find module providing package

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 github.com/marvincaspar/go-example/cmd/server

Similarly, to run your project, you will have to provide module-name/main-package-path:

go run github.com/marvincaspar/go-example/cmd/server

Go clean can be executed in same way, by providing module-name/path-with-main-package

go clean github.com/marvincaspar/go-example/cmd/server

or

GOPATH="$(pwd)/vendor:$(pwd)" GOBIN="$(pwd)/bin" go clean github.com/marvincaspar/go-example/cmd/server 

However, as per https://blog.learngoprogramming.com/code-organization-tips-with-packages-d30de0d11f46, just put your source files into your project’s root. It’s better that way.

Tags:

Go

Go Modules