Go语言 etcd/clientv3报错: etcd undefined: resolver.BuildOption

故障描述

go语言版本

1go version
2go version go1.14 linux/amd64

经过测试,在macOS,Windows,Linux上都有这个问题

包管理器go mod

 1module goAgent
 2
 3go 1.14
 4
 5require (
 6    github.com/Shopify/sarama v1.26.1
 7    github.com/coreos/etcd v3.3.18+incompatible // indirect
 8    github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect
 9    github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
10    github.com/go-ini/ini v1.52.0
11    github.com/gogo/protobuf v1.3.1 // indirect
12    github.com/google/uuid v1.1.1 // indirect
13    github.com/hpcloud/tail v1.0.0
14    go.etcd.io/etcd v3.3.18+incompatible
15    go.uber.org/zap v1.14.0 // indirect
16    google.golang.org/grpc v1.27.1 // indirect
17    gopkg.in/fsnotify.v1 v1.4.7 // indirect
18    gopkg.in/ini.v1 v1.52.0
19    gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
20    )
21

在安装go get go.etcd.io/etcd/clientv3时出错

错误信息:

1# github.com/coreos/etcd/clientv3/balancer/resolver/endpoint
2../../pkg/mod/github.com/coreos/etcd@v3.3.18+incompatible/clientv3/balancer/resolver/endpoint/endpoint.go:114:78: undefined: resolver.BuildOption
3../../pkg/mod/github.com/coreos/etcd@v3.3.18+incompatible/clientv3/balancer/resolver/endpoint/endpoint.go:182:31: undefined: resolver.ResolveNowOption
4# github.com/coreos/etcd/clientv3/balancer/picker
5../../pkg/mod/github.com/coreos/etcd@v3.3.18+incompatible/clientv3/balancer/picker/err.go:37:44: undefined: balancer.PickOptions
6../../pkg/mod/github.com/coreos/etcd@v3.3.18+incompatible/clientv3/balancer/picker/roundrobin_balanced.go:55:54: undefined: balancer.PickOptions

解决方法

将grpc版本替换成v1.26.0版本

具体步骤

  1. 修改依赖为v1.26.0

    1go mod edit -require=google.golang.org/grpc@v1.26.0
    
  2. 下载v1.26.0版本的grpc

    1go get -u -x google.golang.org/grpc@v1.26.0
    
  3. 重新构建程序

    1go build
    

大功告成!

参考

segmentfault - etcd undefined: resolver.BuildOption