【问题标题】:Go get error while getting major version module dependency获取主要版本模块依赖项时出错
【发布时间】:2020-10-28 15:07:21
【问题描述】:

我有一个可执行的 go 模块,我正在尝试执行以下命令

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

并得到这个错误:

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 是一个不可执行的 go 模块,带有以下标签 v1.0.0、v1.0.1 和 v2.0.0。

我是新手。所以有人请告诉我这里有什么问题。

带有主包的模块

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 版本 go1.14.3 darwin/amd64

进入环境

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

我尝试设置 GO111MODULE=on;但这不会改变 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

【问题讨论】:

  • repo 不包含 v2 包。也许你想指定版本 v2:去 github.com/saipraveen-a/number-manipulation@v2。 go.mod 文件必须有:require github.com/saipraveen-a/number-manipulation v2
  • 我得到这个 go get github.com/saipraveen-a/number-manipulation@v2: no matching versions for query "v2"

标签: go go-modules go-get go-build go-packages


【解决方案1】:

您的 github 模块 go.mod 文件如下所示:

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

go 1.14

而您的客户端代码正在导入v2

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

如果要使用标记为v2.0.0 的版本,需要将github 模块的go.mod 文件更改为:

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

go 1.14

请注意,这会迫使您更改库中的导入路径

然后将v2 路径输入到您的客户端 go.mod 文件中:

module main

go 1.14

require github.com/saipraveen-a/number-manipulation/v2 v2.0.0

【讨论】:

  • 我有 v1.0.1 和 v2.0.0 版本的模块。我想在我的主模块中使用这两个版本。 v1.0.1 标签在 master 上,v2.0.0 标签在我的数字操作模块的 v2 分支上
  • @java_geek 无论分支如何,您都试图在客户端代码中导入 v2 路径,而库的 go.mod 文件(在 master 和 v2 分支中)定义了模块作为module github.com/saipraveen-a/number-manipulation。您必须将修复推送到您的 v2 分支并将 v2 后缀添加到 module 路径,否则您不能 require v2.0.0 标记
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-01
  • 2021-12-28
  • 2014-09-09
  • 2018-11-04
  • 2021-12-23
  • 2022-08-03
  • 2021-08-22
相关资源
最近更新 更多