问题描述
我有一个包含文件hello
和go.mod
的软件包hello.go
和一个包含文件say_things
和go.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
文件,然后replace
用current 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