I have created a module with several versions v1.0.0, v1.0.1, v1.1.0, and v2.0.0.
I created a simple application to use the modules. Versions 1.x.x work fine, I cannot get the application to utilize version v2.0.0. Here is the directory structure.
I read this article already Go’s Major Versioning Sucks – From a Fanboy | Lane Wagner
I’m using Go 1.18.
Why is the application using module from version v1.x.x instead of v2.0.0?
// My module published on Github.
gomath/
├── calc
│ └── calc.go
├── geometry
│ └── cube.go
├── go.mod
├── readme.md
└── v2
├── calc
│ └── calc.go
├── geometry
│ └── cube.go
└── go.mod
myfirstapp $ cat main.go
package main
import (
"fmt"
"github.com/brandon/gomath/geometry"
// v2 path
"github.com/brandon/gomath/v2/calc"
// v1 path
//"github.com/brandon/gomath/calc"
)
func main() {
fmt.Println(geometry.CubeVolume(15))
// v2 module method NOT WORKING
s := calc.Add(4, 5, 6, 7)
// v1 module method WORKING
//s := calc.Add(4, 5)
fmt.Println(s)
}
v2 module looks like this.
package calc
func Add(args ...int) int {
sum := 0
for _, v := range args {
sum += v
}
return sum
}
func Sub(x, y int) int {
return x - y
}
The error message shows that the application is still using the module version 1.x.x.
myfirstapp $ go run main.go
# command-line-arguments
./main.go:16:25: too many arguments in call to calc.Add
have (number, number, number, number)
want (int, int)
Go get install v2.0.0 without any issue.
myfirstapp $ ls -l ~/go/pkg/mod/github.com/brandon/gomath/v2@v2.0.0
total 20
dr-x------ 2 brandon brandon 4096 Apr 11 16:20 calc
dr-x------ 2 brandon brandon 4096 Apr 11 16:20 geometry
-r-------- 1 brandon brandon 53 Apr 11 16:20 go.mod
-r-------- 1 brandon brandon 0 Apr 11 16:20 LICENSE
-r-------- 1 brandon brandon 19 Apr 11 16:20 readme.md
dr-x------ 4 brandon brandon 4096 Apr 11 16:20 v2