58

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?

Marvin Caspar
  • 1,319
  • 1
  • 14
  • 25
  • 2
    You either do go modules or GOPATH. If you want to do modules it is best to force module builds with GO111MODULE=on. Your GOPATH looks suspicious, maybe you should switch to modules. Such errors is often a tiny typo; tripple check. – Volker Apr 11 '19 at 12:27
  • I already set the default for GO111MODULE to on. – Marvin Caspar Apr 11 '19 at 12:52
  • You should clearly describe that _only_ `go clean` results in this error. And if you do a module build don't distract by talking about GOPATH or GOBIN. Also note that `go clean` fails if run from a directory without source code (https://github.com/golang/go/issues/31002) . Where are you running go clean? It seems everything is working fine and you try to cleanup somewhere where nothing is to be cleaned. – Volker Apr 11 '19 at 13:02
  • I'm not quite sure what the intent is, but you should almost certainly not set `GOPATH` to include the `vendor` directory (`GOPATH="$(pwd)/vendor:$(pwd)"`). – thepudds Aug 01 '19 at 20:18

6 Answers6

57

I generally use go get and go mod tidy for same. It works all the time.

go mod tidy
BITSSANDESH
  • 1,025
  • 4
  • 13
  • 23
  • 2
    This answer is spot on. Just to add some extra detail from the documentation [here](https://go.dev/ref/mod#go-mod-tidy). `go mod tidy` ensures that the go.mod file matches the source code in the module. It adds any missing module requirements necessary to build the current module’s packages and dependencies, and it removes requirements on modules that don’t provide any relevant packages. It also adds any missing entries to go.sum and removes unnecessary entries. – Stephen Dunne Jul 18 '22 at 09:59
  • In my case I was trying to import an individual `.go` file instead of the folder, if it saves someone else some time – Schaki Jul 15 '23 at 09:18
  • I was having trouble finding some references to other .go package but I already had a go.mod file. What I did was delete the requires (...) and do go mod tidy again. And it worked. – David Merinos Aug 22 '23 at 17:03
38

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

24

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.

ShailyAggarwal
  • 813
  • 9
  • 20
  • 2
    What do you need to change if the github.com/name changes? (Ie. for something you forked?) – dagelf Feb 20 '22 at 20:15
3

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.

JohnAllen
  • 7,317
  • 9
  • 41
  • 65
1

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

0

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.

Taku
  • 5,639
  • 2
  • 42
  • 31