Reputation: 578
I'm setting up a new project using Go modules with this tutorial, and then trying to build it.
The module is located in a folder outside of the $GOPATH with the following structure:
example.com
├── my-project
├── ├── main
├── ├── ├── main.go
├── ├── go.mod
I've run go mod init example.com/my-project
in directory example.com/my-project
and created the go.mod file shown above.
main.go
has basic contents:
package main
import (
"fmt"
)
func main(){
fmt.Println("Hello, world!")
}
After attempting to run go build
in directory example.com/my-project
, I receive the following error message:
can't load package: package example.com/my-project: unknown import path "example.com/my-project": cannot find module providing package example.com/my-project
.
I've also attempted to run go build
in directory /
, outside of example.com/my-project
, and I get similar, failing results:
can't load package: package .: no Go files in ...
I'm probably getting some basic thing wrong, so thanks for your patience and any help you can give.
Upvotes: 20
Views: 25859
Reputation: 21
In my case it was that the variables GOMOD and GOWORK were taking other values different from the project I solved it by executing the command go env
and verifying the values of those variables and deleting the files of that address.
Then I removed the go.mod
and go.sum
file from the project and ran the following commands again:
go mod init projectName
go mod tidy
go run ./...
And it worked perfectly.
Upvotes: 1
Reputation: 1028
no need for the directory main, just move your main.go and go.mod to example.com/my-project and it will work.
Project root should look like:
.
├── go.mod
└── main.go
Upvotes: 5