无法导入gorilla/mux (github.com/gorilla/mux@v1.7.4:在go.mod中明确要求,但在vendor/modules.txt中没有标记为明确要求)

9
在Go语言中,我尝试创建一个简单的数据库连接。我需要导入 gorilla/mux,但是我无法成功导入。
我在使用VS Code。在cd到我的项目目录后,我创建了main.go并执行了以下命令: go get -u github.com/gorilla/mux 下面是main.go的代码:
package main

import (
    "database/sql"
    "fmt"
    "github.com/gorilla/mux"
    _ "github.com/lib/pq"
)

const (
    host     = "localhost"
    port     = 3000
    user     = "postgres"
    password = "postgres"
    dbname   = "test1"
)

func main() {
    psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
        "password=%s dbname=%s sslmode=disable",
        host, port, user, password, dbname)

    db, err := sql.Open("postgres", psqlInfo)
    if err != nil {
        panic(err)
    }
    defer db.Close()

    err = db.Ping()
    if err != nil {
      panic(err)
    }

    fmt.Println("Successfully connected!")
}

请注意,在执行go get -u github.com/gorilla/mux命令后,终端会显示:
C:\Go\src\github.com\IamFrost\go-rest-api>go get -v -u github.com/gorilla/mux
go: golang.org/x/text upgrade => v0.3.2
go: golang.org/x/crypto upgrade => v0.0.0-20200429183012-4b2356b1ed79
go: golang.org/x/sys upgrade => v0.0.0-20200430082407-1f5687305801
go: golang.org/x/net upgrade => v0.0.0-20200425230154-ff2c4b7c35a0
go: downloading golang.org/x/sys v0.0.0-20200430082407-1f5687305801
golang.org/x/sys/cpu
golang.org/x/crypto/chacha20poly1305
crypto/tls

点击查看图片描述 看,我在小地图中没有其他语法错误。当我把鼠标放在红色标记处时,它的悬停文本对我来说很有趣:

1)已导入但未使用

但下一行

2)没有为导入github.com/gorilla/mux的包

哈哈,这不是违背了1)吗?

请有人解释一下为什么会出现这种情况

然而,

终端中使用go build

这里是终端:

C:\Go\src\github.com\IamFrost\go-rest-api>go build
go: inconsistent vendoring in C:\Go\src:
        github.com/gorilla/mux@v1.7.4: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        github.com/lib/pq@v1.4.0: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        golang.org/x/crypto@v0.0.0-20200429183012-4b2356b1ed79: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        golang.org/x/net@v0.0.0-20200425230154-ff2c4b7c35a0: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        golang.org/x/sys@v0.0.0-20200430082407-1f5687305801: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
        golang.org/x/crypto@v0.0.0-20200128174031-69ecbb4d6d5d: is marked as explicit in vendor/modules.txt, but not explicitly required in go.mod
        golang.org/x/net@v0.0.0-20191126235420-ef20fe5d7933: is marked as explicit in vendor/modules.txt, but not explicitly required in go.mod
        golang.org/x/sys@v0.0.0-20200201011859-915c9c3d4ccf: is marked as explicit in vendor/modules.txt, but not explicitly required in go.mod

run 'go mod vendor' to sync, or use -mod=mod or -mod=readonly to ignore the vendor directory

[注意:我执行了 'go mod vendor' 命令,但没有任何变化]
有人可以告诉我为什么我无法导入gorilla/mux或pq。
我还需要做什么吗?
(请解释一下这是什么意思?在go.mod中明确要求,但在vendor/modules.txt中未标记为明确要求)

你可以选择将依赖项打包或不打包,但是你当前的状态使用了打包的软件包和未打包的gorilla/mux,这就是错误信息告诉你的原因。重新打包所有内容。 - Volker
@Volker 怎么做?有示例或建议吗? - user12504785
1
go mod vendor? - Volker
@Volker 我试过了,但没有变化。 - user12504785
4个回答

9
这是我如何解决它的方法:
go get ./...
go mod vendor

如果您的 Go 版本低于 1.13,则应在上述命令中添加 GO111MODULE=on

1

我不知道为什么它能工作(我是初学者)。我在网络上找到了一些东西,某种程度上它就能工作。

  1. 这是我的Go目录 C:\Go\,但我在桌面文件夹中创建了项目。

  2. 然后我在 C:\Go\src 下重新创建了该项目(确切的文件夹是 C:\Go\src\github.com\[github_username]\[project_name])。

  3. 然后我将所有代码粘贴到 main.go 中。

  4. 最后从终端运行 go mod vendor

  5. 然后运行 go build

  6. 然后运行 go run main.go

  7. 最后它就能工作了。

[我想知道为什么它能工作。非常感谢您的评论:为什么他们强制让我去 C:\Go\src。我已经在环境变量中设置了 GOPATHC:\Go\GOBINC:\Go\bin]


0
在最近的Golang版本(1.11及以上)中,GOPATH变量发生了变化。我的理解是,只有当GOPATH不是$HOME/go时才需要设置它。
Go会在$HOME/go/src/中查找您的项目,例如我的Golang项目位于$Home/go/src/github.com/目录等。
如果您想使用其他位置的项目,仍然可以将GOPATH设置为其他目录。
我的shell文件包含以下内容,以适应已设置和未设置GOPATH的情况。
export PATH="$PATH:$(go env GOPATH)/bin"
您可以在GOPATH环境文档中获取更多信息。

0

go mod init,go mod tidy,go run :3

我在默认目录(C:\ Go \ src [projectName])中工作,并在项目目录中运行命令。如果这不能帮助您,请将项目移动到您的GOPATH。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接