简体   繁体   中英

go mod: cannot find module providing package

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?

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?

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?

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?

在您的情况下,最好运行go fmt ./... ,这意味着将go fmt应用于所有软件包。

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: go mod init your-program.go

Step 2: go mod tidy

Step 3: sudo go build your-program.go

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM