Reputation: 1409
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 github.com/marvincaspar/go-example: unknown import path "github.com/marvincaspar/go-example": cannot find module providing package github.com/marvincaspar/go-example
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 github.com/marvincaspar/go-example
go 1.12
require (
github.com/go-sql-driver/mysql v1.4.1
)
And finally the main.go
file:
package main
import (
"fmt"
"os"
"github.com/marvincaspar/go-example/pkg/storage/mysql"
)
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?
Upvotes: 67
Views: 264043
Reputation: 41
Also check if there are non *.go
files in the current dir. This probelm occurred to me when I tried to run the command while not properly organising the files, as a result in the current dir there is a folder containing .tmpl
files.
Upvotes: 0
Reputation: 5927
In my case, this was failing in CircleCI and I could not produce locally. Here was my scenario.
App A depends on Module B
Module B depends on Module C
App A directly depends on Module C
However, the module C's version was different in App A than in Module B. This was the reason for this error and by fixing this in Module B by running go get -u
, publishing a new version, and pulling it down in App A fixed it for me.
Upvotes: 0
Reputation: 7531
This can also happen if you are using workspaces. It seems like you can't use one package without workspaces if you are using others with workspaces.
So try going into your top level workspace and do
go work use ./problemPackage
.
At least this worked for me.
Upvotes: 7
Reputation: 754
To solve this problem you have to do few things, First, go to the project directory via the Terminal
then run the following command ( If you are using git clone then go to the clone directory folder via Terminal and run the following command):
Step 1: sudo go mod init your-program.go
Step 2: sudo go mod tidy
Step 3: sudo go build your-program.go
Upvotes: -5
Reputation: 1135
I generally use go get and go mod tidy for same. It works all the time.
go mod tidy
Upvotes: 68
Reputation: 989
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
Upvotes: 40
Reputation: 863
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.
Upvotes: 25