我有一个包hello
,其中包含文件go.mod
和hello.go
,还有一个包say_things
,其中包括文件go.mod
和say_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.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
代码?
通过执行以下更改来更新代码的方法。
在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
go-get命令下载所需模块的新版本。例如:
% go get -u all
go: github.com/user/say_things upgrade => v0.0.0-<new hash>
–将最后一个模块的所有版本下载到$GOPATH/pkg
,并升级go.mod
文件。
❕当使用go模块时,更好的方法是向存储库添加版本标记(tag
必须符合语义版本规范(
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