golang:如何在go-mod中指定包的版本號?
今天遇到了一個小坑:使用etcdv3的時候,報了這么一堆錯誤
# github.com/coreos/etcd/clientv3/balancer/resolver/endpointvendor\github.com\coreos\etcd\clientv3\balancer\resolver\endpoint\endpoint.go:114:78: undefined: resolver.BuildOption
vendor\github.com\coreos\etcd\clientv3\balancer\resolver\endpoint\endpoint.go:182:31: undefined: resolver.ResolveNowOption
# github.com/coreos/etcd/clientv3/balancer/pickervendor\github.com\coreos\etcd\clientv3\balancer\picker\err.go:37:44: undefined: balancer.PickOptions
vendor\github.com\coreos\etcd\clientv3\balancer\picker\roundrobin_balanced.go:55:54: undefined: balancer.PickOptions
上網查了一下原因,說是最新的v1.27.0版本的google.golang.org/grpc包不支持etcdv3。同時網上也給出了解決方案:將grpc1.27.0 替換成grpc1.26.0版本(具體操作是手動在go.mod的require下修改google.golang.org/grpc v1.26.0,或者直接用命令 go mod edit -require=google.golang.org/grpc@v1.26.0)
but~~~~實際操作后發現,替換包版本號的方案是對的,但是操作不正確,修改require不能指定包的版本號!
正確的做法是,在go.mod中用replace指定包版本號,比如:
replace google.golang.org/grpc => google.golang.org/grpc v1.26.0
然后再go run或go build,一切完美~~
鏈接:https://www.jianshu.com/p/1971a27096b9