Go项目不从github提取导入更新

问题描述

我有一个包含文件hellogo.mod的软件包hello.go和一个包含文件say_thingsgo.mod的软件包say_things.go。 / p>

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,期望它说“ GO AWAY”,但事实并非如此。它仍然显示BYE。我删除了生成的go.sum并再次删除了go 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.mod项目中打开您的hello文件,然后replacecurrent version的最后一个提交哈希值对github.com/user/say_things编写的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
,

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...