2

I have a package hello which contains the files go.mod and hello.go and a package say_things with files go.mod and 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")
}

Both these projects are github projects. When I run hello.go, it prints "BYE" as expected. I now update SayBye to be:

package say_things

import "fmt"

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

and push the changes to github. I again run hello.go, expecting it to say "GO AWAY", but it doesn't. It still says BYE. I remove the go.sum generated and go run hello.go again, but still it says BYE. I then go to go/pkg/mod/github.com/user/ and delete say_bye@v0.0.0-<hash>, and run hello.go again. Still, nothing changes. Next, I run go get github.com/user/say_things, still I get BYE.

How do I get hello.go to run the updated say_hello code?

kozmo
  • 4,024
  • 3
  • 30
  • 48
fraiser
  • 929
  • 12
  • 28
  • 1
    Run the go get command with the update flag: `go get -u github.com/user/say_things` to get the updated package. – Charlie Tumahai Sep 03 '20 at 19:25
  • 4
    Also, `go.sum` does not dictate the version, it only records the checksums. `go.mod` is what defines the version you are using. – JimB Sep 03 '20 at 19:27
  • @MuffinTop Forgot to mention in the post, I did that too, didn't work. – fraiser Sep 03 '20 at 19:32
  • @JimB Deleting the versions in `go.mod` worked, thanks! Is there a way to automatically pull updates? – fraiser Sep 03 '20 at 19:34
  • 2
    @picotard: `go get -u` will check for updates, but your random commit ids on master do not constitute "updates". If you want a specific commit, you need to `go get` that commit. – JimB Sep 03 '20 at 19:37
  • 2
    Once you reach `v1.0` and you push new commits after that, `go get -u` will not fetch them. You have to use `go get foo/bar@master`. See [Go modules pulls old version of a package](https://stackoverflow.com/questions/57722865/go-modules-pulls-old-version-of-a-package/57722999#57722999). – icza Sep 03 '20 at 19:43
  • I wish there was a way to specify pulling from master in `go.mod` instead of from a specific version or commit, but it seems like `go get -u github.com/user/say_things@master` works well for now. – fraiser Sep 03 '20 at 19:53

2 Answers2

3

A way to update your code by doing the following change.

Open your go.mod file in your hello project and replace the current version written against github.com/user/say_things with the last commit hash of your say_things project.

In other words, in go.mod file
Replace github.com/user/say_things <current-version>
with github.com/user/say_things <last-commit-hash>

And finally run:

$ go mod tidy
$ go mod vendor
Masudur Rahman
  • 1,585
  • 8
  • 16
3

go get command downloads new version of required module. For example:

% go get -u all

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

– download all last module's version to $GOPATH/pkg and upgrade go.mod file.

❕When using go-modules, the better approach is adding version tags to repository (tag have to fit to Semantic Versioning specification)

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

This will allow you to manual change versions in 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

, and get required version by version tag

% 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
kozmo
  • 4,024
  • 3
  • 30
  • 48