【问题标题】:Go project not pulling import updates from githubGo 项目不从 github 拉取导入更新
【发布时间】:2020-09-03 19:19:31
【问题描述】:

我有一个包hello,其中包含文件go.modhello.go,还有一个包say_things,其中包含文件go.modsay_things.go

hello.go:

package main

import "github.com/user/say_things"

func main() {
        say_things.SayBye()
}

say_things.go:

package say_things

import "fmt"

func SayBye() {
        fmt.Println("BYE")
}

这两个项目都是 github 项目。当我运行hello.go 时,它会按预期打印“BYE”。我现在将SayBye 更新为:

package say_things

import "fmt"

func SayBye() {
        fmt.Println("GO AWAY")
}

并将更改推送到 github。我再次运行hello.go,期待它说“走开”,但事实并非如此。它仍然显示BYE。我再次删除了生成的go.sumgo run hello.go,但它仍然显示BYE。然后我转到go/pkg/mod/github.com/user/ 并删除say_bye@v0.0.0-<hash>,然后再次运行hello.go。尽管如此,什么都没有改变。接下来,我运行go get github.com/user/say_things,仍然得到BYE

如何让hello.go 运行更新后的say_hello 代码?

【问题讨论】:

  • 运行带有更新标志的 go get 命令:go get -u github.com/user/say_things 以获取更新的包。
  • 另外,go.sum 不指定版本,它只记录校验和。 go.mod 定义了您正在使用的版本。
  • @MuffinTop 帖子里忘记说了,我也做了,没用。
  • @picotard: go get -u 将检查更新,但您在 master 上的随机提交 id 并不构成“更新”。如果你想要一个特定的提交,你需要go get那个提交。
  • 一旦您到达v1.0 并在此之后推送新的提交,go get -u 将不会获取它们。你必须使用go get foo/bar@master。见Go modules pulls old version of a package

标签: go versioning dependency-management go-modules


【解决方案1】:

一种通过执行以下更改来更新代码的方法。

在您的hello 项目中打开您的go.mod 文件,并在replace 中使用github.com/user/say_things 编写的current version 以及您的say_things 项目的最后一次提交哈希。

换句话说,在go.mod 文件中
替换github.com/user/say_things <current-version>
github.com/user/say_things <last-commit-hash>

最后运行:

$ go mod tidy
$ go mod vendor

【讨论】:

  • 添加vendor目录的目的是什么?
  • 如果您想将所有依赖项保留在项目中,vendor 目录将完成这项工作。如果你不想要vendor 目录,你可以运行go mod tidy
【解决方案2】:

go get 命令下载所需模块的新版本。 例如:

% go get -u all

go: github.com/user/say_things upgrade => v0.0.0-<new hash>

– 将所有最后一个模块的版本下载到$GOPATH/pkg 并升级go.mod 文件。

❕当使用go-modules 时,更好的方法是向存储库添加版本标签(tag 必须 符合Semantic Versioning 规范)

git commit -a - m "say_things - some changes"
git tag v1.0.1
git push
git push --tags 

这将允许您手动更改 go.mod 中的版本

module github.com/user/hello

go 1.15

require github.com/user/say_things v1.0.1
% go mod download 

go: finding github.com/user/say_things v1.0.1

,通过版本标签获取需要的版本

% go get github.com/user/say_things@v1.0.1

go: finding github.com/user/say_things v1.0.1
go: downloading github.com/user/say_things v1.0.1
go: extracting github.com/user/say_things v1.0.1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-09
    • 2015-06-25
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    • 2016-12-27
    • 1970-01-01
    • 2018-10-15
    相关资源
    最近更新 更多