Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

go mod: cannot find module providing package

Tags:

go

go-modules

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?

like image 903
Marvin Caspar Avatar asked Apr 11 '19 11:04

Marvin Caspar


People also ask

Where do go modules get installed?

All downloaded modules are cached locally in your $GOPATH/pkg/mod directory by default. If you import a package to your project without downloading it first using go get , the latest tagged version of the module providing that package will be installed automatically and added to your go.

Is go mod mandatory?

Thanks, I transferred the accepted answer to this one. The part "A module is defined by a UTF-8 encoded text file named go. mod in its root directory." sounds like go. mod is mandatory.

What is the difference between go module and package?

In Go, a package is a directory of .go files, and packages form the basic building blocks of a Go program. Using packages, you organize your code into reusable units. A module, on the other hand, is a collection of Go packages, with dependencies and versioning built-in.

What is 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, if there are some not used dependencies go mod tidy will remove those from go.


Video Answer


3 Answers

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

like image 177
Nicholas Fernandes Paolillo Avatar answered Oct 17 '22 07:10

Nicholas Fernandes Paolillo


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

go mod tidy
like image 31
BITSSANDESH Avatar answered Oct 17 '22 07:10

BITSSANDESH


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.

like image 22
ShailyAggarwal Avatar answered Oct 17 '22 08:10

ShailyAggarwal