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

10 go gorilla pq

在 Go 中,我尝试了简单的数据库连接。我需要导入gorilla/mux,但我不能。

我正在使用 VS 代码。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!")
}
Run Code Online (Sandbox Code Playgroud)

[注意,执行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
Run Code Online (Sandbox Code Playgroud)

]

在此输入图像描述 看看我在迷你地图中没有其他语法错误。在红色标记处,当我放置鼠标时,悬停文本对我来说很有趣:

1)imported but not used

但下一行

2) no package for import 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
Run Code Online (Sandbox Code Playgroud)

[注意:我也做了“go modvendor”,但没有改变]

所以有人指出我为什么不能导入gorilla/muxpq.

我还需要做什么?

(请解释一下这是什么意思?is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt

Abh*_*yap 10

以下是我为自己修复的方法:

go get ./...
go mod vendor
Run Code Online (Sandbox Code Playgroud)

如果你的 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。我已经在环境变量中设置了GOPATH C:\Go\GOBIN ] C:\Go\bin