【解决了一个小问题】golang的go.mod中出现版本错误

代码中的这一句使用prometheus2.28.0版本的代码:

import "github.com/prometheus/prometheus/prompb"

我把require github.com/prometheus/prometheus v2.5.0+incompatible修改为github.com/prometheus/prometheus v2.28.0
于是出现了下面的错误:

GOROOT=C:Go #gosetup
GOPATH=C:Usersahfuzhanggo #gosetup
C:Goingo.exe list -modfile=D:/source/ahfuzhang/daily_coding/2021-08-13/remote_write_client/go.mod -m -json -mod=mod all #gosetup
go: errors parsing go.mod:
D:/source/ahfuzhang/daily_coding/2021-08-13/remote_write_client/go.mod:12: require github.com/prometheus/prometheus: version "v2.28.0" invalid: module contains a go.mod file, so major version must be compatible: should be v0 or v1, not v2

到官网看,这个版本肯定是存在的!怎么样才能引用最新的版本呢?

go.mod中使用commit id来代替:
打开:https://github.com/prometheus/prometheus/tags
选择v2.28.0的commit id: ff58416
修改go.mod为 require github.com/prometheus/prometheus ff58416
编译代码后,自动修改为了require github.com/prometheus/prometheus v1.8.2-0.20210811141203-dcb07e8eac34

搞定

原文地址:https://www.cnblogs.com/ahfuzhang/p/15156575.html