Go project not pulling import updates from github

Viewed 1858

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?

2 Answers

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

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
Related