执行go mod tidy时go.mod自动升级Go版本的问题求助
问题描述
我有如下go.mod文件,在VSCode中运行模块时编译器提示要执行go mod tidy。执行该命令后,go.mod里的Go版本从1.19升级到1.21,还新增了toolchain go1.22.1行,go.sum也同步更新。同样的go.mod文件同事用着没问题,说明是我本地配置的问题。执行go mod tidy -go=1.19时出现错误:
go: google.golang.org/grpc@v1.65.0 requires go@1.21, but 1.19 is requested
我的go.mod内容:
module product-api go 1.19 require ( cloud.google.com/go/pubsub v1.37.0 github.com/DataDog/datadog-go/v5 v5.2.0 github.com/fermatcommerce/fermat/go/common v0.0.0-20220906110310-ef66faa0b8db github.com/fermatcommerce/fermat/go/product-svc v0.0.0-00010101000000-000000000000 github.com/fermatcommerce/fermat/proto/product-service/go v0.0.0-00010101000000-000000000000 github.com/go-chi/chi/v5 v5.0.8 github.com/go-chi/cors v1.2.1 github.com/google/uuid v1.6.0 github.com/lib/pq v1.10.6 github.com/shopspring/decimal v1.3.1 github.com/stretchr/testify v1.8.4 go.uber.org/zap v1.24.0 golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 google.golang.org/protobuf v1.34.2 ) require ( cloud.google.com/go v0.112.1 // indirect cloud.google.com/go/cloudsqlconn v0.5.1 // indirect cloud.google.com/go/compute/metadata v0.3.0 // indirect cloud.google.com/go/iam v1.1.6 // indirect github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect github.com/Microsoft/go-winio v0.5.2 // indirect github.com/Microsoft/hcsshim v0.9.4 // indirect github.com/cenkalti/backoff/v4 v4.1.3 // indirect github.com/containerd/cgroups v1.0.4 // indirect github.com/containerd/containerd v1.6.8 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/docker/distribution v2.8.1+incompatible // indirect github.com/docker/docker v20.10.17+incompatible // indirect github.com/docker/go-connections v0.4.0 // indirect github.com/docker/go-units v0.5.0 // indirect github.com/felixge/httpsnoop v1.0.4 // indirect github.com/go-logr/logr v1.4.1 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect github.com/golang/protobuf v1.5.4 // indirect github.com/google/s2a-go v0.1.7 // indirect github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect github.com/googleapis/gax-go/v2 v2.12.2 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect github.com/jackc/pgx/v4 v4.17.0 // indirect github.com/jackc/pgx/v5 v5.5.2 // indirect github.com/jackc/puddle/v2 v2.2.1 // indirect github.com/magiconair/properties v1.8.6 // indirect github.com/moby/sys/mount v0.3.3 // indirect github.com/moby/sys/mountinfo v0.6.2 // indirect github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect github.com/morikuni/aec v1.0.0 // indirect github.com/opencontainers/go-digest v1.0.0 // indirect github.com/opencontainers/image-spec v1.0.3-0.20211202183452-c5a74bcca799 // indirect github.com/opencontainers/runc v1.1.3 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rogpeppe/go-internal v1.12.0 // indirect github.com/sirupsen/logrus v1.8.1 // indirect github.com/testcontainers/testcontainers-go v0.15.0 // indirect go.opencensus.io v0.24.0 // indirect go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect go.opentelemetry.io/otel v1.24.0 // indirect go.opentelemetry.io/otel/metric v1.24.0 // indirect go.opentelemetry.io/otel/trace v1.24.0 // indirect go.uber.org/atomic v1.10.0 // indirect go.uber.org/multierr v1.9.0 // indirect golang.org/x/crypto v0.23.0 // indirect golang.org/x/net v0.25.0 // indirect golang.org/x/oauth2 v0.20.0 // indirect golang.org/x/sync v0.7.0 // indirect golang.org/x/sys v0.20.0 // indirect golang.org/x/text v0.15.0 // indirect golang.org/x/time v0.5.0 // indirect google.golang.org/api v0.169.0 // indirect google.golang.org/genproto v0.0.0-20240213162025-012b6fc9bca9 // indirect google.golang.org/genproto/googleapis/api v0.0.0-20240528184218-531527333157 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 // indirect google.golang.org/grpc v1.65.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect )
我的go env输出:
GO111MODULE='' GOARCH='arm64' GOBIN='' GOCACHE='/Users/tanmay/Library/Caches/go-build' GOENV='/Users/tanmay/Library/Application Support/go/env' GOEXE='' GOEXPERIMENT='' GOFLAGS='' GOHOSTARCH='arm64' GOHOSTOS='darwin' GOINSECURE='' GOMODCACHE='/Users/tanmay/go/pkg/mod' GONOPROXY='' GONOSUMDB='' GOOS='darwin' GOPATH='/Users/tanmay/go' GOPRIVATE='' GOPROXY='https://proxy.golang.org,direct' GOROOT='/opt/homebrew/Cellar/go/1.22.1/libexec' GOSUMDB='sum.golang.org' GOTMPDIR='' GOTOOLCHAIN='local' GOTOOLDIR='/opt/homebrew/Cellar/go/1.22.1/libexec/pkg/tool/darwin_arm64' GOVCS='' GOVERSION='go1.22.1' GCCGO='gccgo' AR='ar' CC='cc' CXX='c++' CGO_ENABLED='1' GOMOD='/Users/tanmay/Desktop/fermat/go/product-api/go.mod' GOWORK='' CGO_CFLAGS='-O2 -g' CGO_CPPFLAGS='' CGO_CXXFLAGS='-O2 -g' CGO_FFLAGS='-O2 -g' CGO_LDFLAGS='-O2 -g' PKG_CONFIG='pkg-config' GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/tk/g6yl6ybj5gz764nfsh8zrfh00000gn/T/go-build2365762375=/tmp/go-build -gno-record-gcc-switches -fno-common'
原因分析
- 本地Go版本过高:当前使用的是Go1.22.1,而项目要求Go1.19。高版本Go执行
go mod tidy时,会自动识别依赖的最低Go版本要求(比如google.golang.org/grpc@v1.65.0要求Go1.21),因此会升级go.mod中的Go版本以满足依赖要求。 - 工具链配置问题:
GOTOOLCHAIN设置为local,意味着使用本地安装的高版本Go工具链处理依赖,遵循高版本的模块规则,自动升级go.mod版本并添加工具链声明。 - 依赖版本冲突:
google.golang.org/grpc@v1.65.0的最低兼容Go版本是1.21,和项目指定的1.19冲突,所以执行go mod tidy -go=1.19时会报错。
解决办法
1. 切换到Go1.19版本
如果是Homebrew安装的Go,执行以下命令切换:
brew install go@1.19 brew unlink go && brew link go@1.19 --force
切换后用go version确认版本为1.19,再运行go mod tidy即可保持go.mod版本不变。
2. 临时指定工具链执行
无需切换全局版本,临时指定工具链(确保本地已安装Go1.19.x):
GOTOOLCHAIN=go1.19.13 go mod tidy -go=1.19
也可以在go.mod中添加工具链声明,强制使用1.19版本:
toolchain go1.19.13
3. 降级依赖版本
找到google.golang.org/grpc兼容Go1.19的版本(比如v1.59.0),修改go.mod中的间接依赖版本后执行go mod tidy -go=1.19:
require google.golang.org/grpc v1.59.0 // indirect
4. 使用Go版本管理工具
安装gvm快速切换版本:
# 安装gvm bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer) # 安装并切换到Go1.19 gvm install go1.19.13 gvm use go1.19.13 --default
内容的提问来源于stack exchange,提问作者10may
相关产品推荐
相关产品推荐

