Search code examples
gogo-modulesgo-getgo-buildgo-packages

Go get error while getting major version module dependency


I have an executable go module and i am trying to execute the below command

go get github.com/saipraveen-a/number-manipulation/v2

and get this error:

module github.com/saipraveen-a/number-manipulation@upgrade found (v1.0.1), but does not contain package github.com/saipraveen-a/number-manipulation/v2

number-manipulation is a non-executable go module with the following tags v1.0.0, v1.0.1 and v2.0.0.

I am new to go. So someone please tell me what is the issue here.

Module with main package

app.go

package main

import (
    "fmt"

    "github.com/saipraveen-a/number-manipulation/calc"
    calcNew "github.com/saipraveen-a/number-manipulation/v2/calc"
)

func main() {
    result := calc.Add(1, 2)
    fmt.Println("calc.Add(1,2) =>", result)

    result = calc.Add(1, 2, 3, 4, 5)
    fmt.Println("calc.Add(1,2,3,4,5) =>", result)

    newResult, err = calcNew.Add()

    if err != nil {
        fmt.Println("Error: =>", error)
    } else {
        fmt.Println("calcNew.Add(1,2,3,4) =>", calcNew.Add(1, 2, 3, 4))
    }
}

go.mod

module main

go 1.14

require github.com/saipraveen-a/number-manipulation v1.0.1

go version go1.14.3 darwin/amd64

go env

GO111MODULE=""
GOPATH="/Users/<user-id>/Golang"
GOMOD="/Users/<user-id>/GoModules/main/go.mod"

I tried set GO111MODULE=on; but that doesnt change the value of GO111MODULE

# go build app.go 

go: finding module for package github.com/saipraveen-a/number-manipulation/v2/calc

app.go:7:2: module github.com/saipraveen-a/number-manipulation@latest found (v1.0.1), but does not contain package github.com/saipraveen-a/number-manipulation/v2/calc

Solution

  • Your github module go.mod file looks like:

    module github.com/saipraveen-a/number-manipulation
    
    go 1.14
    

    Whereas your client code is importing v2:

    calcNew "github.com/saipraveen-a/number-manipulation/v2/calc"
    

    If you want to use the version tagged v2.0.0 you need to change the github module's go.mod file to:

    module github.com/saipraveen-a/number-manipulation/v2
    
    go 1.14
    

    Note that this forces you to change also import paths within the library itself.

    And then require the v2 path into your client go.mod file:

    module main
    
    go 1.14
    
    require github.com/saipraveen-a/number-manipulation/v2 v2.0.0