go - 为什么 go get -u 在模块目录中需要很长时间,但在 golang 中却很快完成?

标签 go

我的网络非常慢且不稳定。

本地运行的第一次观察

例如,当我在新模块目录(有 main.go)中运行 go get -u github.com/jinzhu/gorm 时。它打印出许多行,并且由于缓慢不稳定的网络而永远无法在我的本地环境中成功完成。

但是如果我在模块目录之外运行相同的命令,它可以在合理的时间内完成而无需任何标准输出。

第二次观察远程运行

我有一个位于美国的远程 VPS 服务器。我在那里创建相同的模块目录并在远程模块中运行相同的命令:go get -u github.com/jinzhu/gormgo.mod 将具有以下内容:

require (
    cloud.google.com/go v0.39.0 // indirect
    github.com/denisenkom/go-mssqldb v0.0.0-20190515213511-eb9f6a1743f3 // indirect
    github.com/golang/mock v1.3.1 // indirect
    github.com/google/btree v1.0.0 // indirect
    github.com/google/pprof v0.0.0-20190515194954-54271f7e092f // indirect
    github.com/jinzhu/gorm v1.9.8 // indirect
    github.com/jinzhu/inflection v0.0.0-20190603042836-f5c5f50e6090 // indirect
    github.com/kr/pty v1.1.4 // indirect
    github.com/lib/pq v1.1.1 // indirect
    go.opencensus.io v0.22.0 // indirect
    golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5 // indirect
    golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522 // indirect
    golang.org/x/image v0.0.0-20190523035834-f03afa92d3ff // indirect
    golang.org/x/lint v0.0.0-20190409202823-959b441ac422 // indirect
    golang.org/x/mobile v0.0.0-20190509164839-32b2708ab171 // indirect
    golang.org/x/mod v0.1.0 // indirect
    golang.org/x/net v0.0.0-20190603091049-60506f45cf65 // indirect
    golang.org/x/oauth2 v0.0.0-20190523182746-aaccbc9213b0 // indirect
    golang.org/x/sys v0.0.0-20190602015325-4c4f7f33c9ed // indirect
    golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
    golang.org/x/tools v0.0.0-20190603152906-08e0b306e832 // indirect
    google.golang.org/appengine v1.6.0 // indirect
    google.golang.org/genproto v0.0.0-20190530194941-fb225487d101 // indirect
    google.golang.org/grpc v1.21.0 // indirect
    honnef.co/go/tools v0.0.0-20190602125119-5a4a2f4a438d // indirect
)

然后我运行 go mod tidygo.mod 变成:

require (
    cloud.google.com/go v0.39.0 // indirect
    github.com/denisenkom/go-mssqldb v0.0.0-20190515213511-eb9f6a1743f3 // indirect
    github.com/google/go-cmp v0.3.0 // indirect
    github.com/jinzhu/gorm v1.9.8
    github.com/jinzhu/inflection v0.0.0-20190603042836-f5c5f50e6090 // indirect
    github.com/lib/pq v1.1.1 // indirect
    golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5 // indirect
    google.golang.org/appengine v1.6.0 // indirect
)

但是如果我从 go.mod 中完全删除 require(...) 部分然后执行 go mod tidy,我得到go.mod 中的以下内容:

require github.com/jinzhu/gorm v1.9.8

只需要一个。通过这一行,我运行 go build,模块可以构建并成功运行,没有任何问题。

所以我很困惑 go get -u 在我的模块目录中到底做了什么。

我的两个问题:

  • 我应该在模块目录内运行 go get -u 还是可以在模块目录外运行它?

  • 为什么模块目录外的go get -u可以轻松完成?与在模块目录中运行相比,它是否执行相同的下载/升级?

我使用的是最新的稳定版 golang go1.12.5。因为 go get -u 调用了 gitgit 调用了 curl,但是 curl 是在不稳定的网络上非常糟糕(与 wget 相比)。我无法在模块目录中完成 go get -u。我会收到很多 error: RPC failed;远端意外挂断;致命的:早期的 EOF;致命:索引包失败;。我不知道这个问题是否有解决方法。但是我可以在模块目录之外使用 go get -u

(顺带一提,如果go get可以使用wget的话,在我的本地环境下就可以了不幸的是,wget 不是当前开发世界的首选工具)。

最佳答案

Should I run go get -u inside module directory or can I simply run it outside module directory?

如果您正在使用模块,如果您希望它实际更新您的 go.mod/go.sum 文件,则需要在模块目录中运行它。

Why go get -u outside the module directory can complete easily? Dose it do the same download/upgrade compared to running inside the module directory?

它们的行为非常不同。 go help getgo help module-getgo help modules 应该解释一些差异,在你的情况下检查 the module doc :

A common mistake is thinking go get -u foo solely gets the latest version of foo. In actuality, the -u in go get -u foo or go get -u foo@latest means to also get the latest versions for all of the direct and indirect dependencies of foo.

gorm 的 go.mod 包含很多东西 ( https://github.com/jinzhu/gorm/blob/master/go.mod ),包括它支持的每个可能的数据库的驱动程序,这看起来非常紧张。因为你只是在做一个全面的 -u 而没有指定 @v1.9.3 或其他任何东西,它正在尝试更新 Gorm 的所有依赖项(以及依赖项的依赖项!)在模块感知模式下。

关于go - 为什么 go get -u 在模块目录中需要很长时间,但在 golang 中却很快完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56431721/

相关文章:

logging - 在 golang 中将 panic 输出重定向到带有时间戳的文件

json - 将多维 JSON 文件映射到 Go 结构

go - 无法使用 goprotobuf 解码消息

string - 为什么拆分字符串本身会返回一个长度为 2 的空 slice ?

file - 如何在 Go 中的 io.Reader 上测试 EOF?

go - 如何确保在结构中定义字段?

MongoDB - Golang 库

Go AWS MQTT 连接在订阅完成前丢失

go - 打断休眠的 goroutine?

go - 如何将当前年份添加到 Go 模板中?