20

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nicholas
  • 570
  • 1
  • 5
  • 15
  • 11
    You can't run `go build` in `example.com/my-project`, because there's nothing to build. The main package you're building is called `example.com/my-project/main` – JimB Jul 01 '19 at 16:53
  • 2
    Oh, I see! If you make that into an answer I'll vote for it. – Nicholas Jul 01 '19 at 19:30
  • 1
    [This question is being discussed on meta.](https://meta.stackoverflow.com/questions/386846/how-can-i-improve-this-downvoted-question) – Script47 Jul 05 '19 at 08:15

2 Answers2

4

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
Itamar Lavender
  • 959
  • 7
  • 20
1

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.