gpt4 book ai didi

go - 无法导入 gorilla/mux(github.com/gorilla/mux@v1.7.4 : is explicitly required in go. mod,但在 vendor/modules.txt 中未标记为显式)

转载 作者:行者123 更新时间:2023-12-01 21:10:39 27 4
gpt4 key购买 nike

在 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!")
}

[注意,在执行 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

]

enter image description here
看我在迷你 map 中没有其他语法错误。在红色标记处,当我放鼠标时,悬停文本对我来说很有趣:

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

[注意:我也做了'go mod vendor',但没有变化]

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

我还需要做什么?

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

最佳答案

这是我为我修复它的方法:

go get ./...
go mod vendor
如果你的 Go 版本小于 1.13那么,你应该在前面加上 GO111MODULE=on 在上面的命令中。

关于go - 无法导入 gorilla/mux(github.com/gorilla/mux@v1.7.4 : is explicitly required in go. mod,但在 vendor/modules.txt 中未标记为显式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61520388/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com