golang安装etcd第三方库clientv3 报错

golang安装etcd第三方库clientv3 报错

问题描述

测试程序额源码:

package main

import (
"context"
"fmt"
"time"

"go.etcd.io/etcd/clientv3"
)

// etcd client put/get demo
// use etcd/clientv3

func main() {
	cli, err := clientv3.New(clientv3.Config{
		Endpoints:   []string{"127.0.0.1:2379"},
		DialTimeout: 5 * time.Second,
	})
	if err != nil {
		// handle error!
		fmt.Printf("connect to etcd failed, err:%v\n", err)
		return
	}
	fmt.Println("connect to etcd success")
	defer cli.Close()
	// put
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	_, err = cli.Put(ctx, "q1mi", "dsb")
	cancel()
	if err != nil {
		fmt.Printf("put to etcd failed, err:%v\n", err)
		return
	}
	// get
	ctx, cancel = context.WithTimeout(context.Background(), time.Second)
	resp, err := cli.Get(ctx, "q1mi",clientv3.WithPrefix())
	cancel()
	if err != nil {
		fmt.Printf("get from etcd failed, err:%v\n", err)
		return
	}
	for _, ev := range resp.Kvs {
		fmt.Printf("%s:%s\n", ev.Key, ev.Value)
	}
}

在go 1.13安装etcd第三方库clientv3时,在执行完go get go.etcd.io/etcd/clientv3后报错

E:\goProject\src\github.com\etcd_demo>go get go.etcd.io/etcd/clientv3
go: finding github.com/coreos/pkg latest
go: finding github.com/coreos/go-systemd latest
# github.com/coreos/etcd/clientv3/balancer/resolver/endpoint
..\..\..\pkg\mod\github.com\coreos\etcd@v3.3.20+incompatible\clientv3\balancer\resolver\endpoint\endpoint.go:114:78: undefined: resolver.BuildOption
..\..\..\pkg\mod\github.com\coreos\etcd@v3.3.20+incompatible\clientv3\balancer\resolver\endpoint\endpoint.go:182:31: undefined: resolver.ResolveNowOpt
ion
tv3\balancer\picker\roundrobin_balanced.go:55:54: undefined: balancer.PickOptions

此时go.mod文件内容为:

module github.com/etcd_demo

go 1.13

require (
	github.com/coreos/bbolt v1.3.4 // indirect
	github.com/coreos/etcd v3.3.20+incompatible // indirect
	github.com/coreos/go-semver v0.3.0 // indirect
	github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf // indirect
	github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
	github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
	github.com/gogo/protobuf v1.3.1 // indirect
	github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
	github.com/google/btree v1.0.0 // indirect
	github.com/google/uuid v1.1.1 // indirect
	github.com/gorilla/websocket v1.4.2 // indirect
	github.com/grpc-ecosystem/go-grpc-middleware v1.2.0 // indirect
	github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
	github.com/grpc-ecosystem/grpc-gateway v1.14.4 // indirect
	github.com/jonboulle/clockwork v0.1.0 // indirect
	github.com/prometheus/client_golang v1.6.0 // indirect
	github.com/soheilhy/cmux v0.1.4 // indirect
	github.com/tmc/grpc-websocket-proxy v0.0.0-20200427203606-3cfed13b9966 // indirect
	github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
	go.etcd.io/bbolt v1.3.4 // indirect
	go.etcd.io/etcd v3.3.20+incompatible
	go.uber.org/zap v1.15.0 // indirect
	golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 // indirect
	google.golang.org/grpc v1.29.1 // indirect
	sigs.k8s.io/yaml v1.2.0 // indirect
)

此时执行程序报如下错误:

E:\goProject\src\github.com\etcd_demo>go run main.go
go: finding google.golang.org/genproto v0.0.0-20190927181202-20e1ac93f88c
go: finding golang.org/x/net v0.0.0-20191002035440-2ec189313ef0
# github.com/coreos/etcd/clientv3/balancer/resolver/endpoint
..\..\..\pkg\mod\github.com\coreos\etcd@v3.3.20+incompatible\clientv3\balancer\resolver\endpoint\endpoint.go:114:78: undefined: resolver.BuildOption
..\..\..\pkg\mod\github.com\coreos\etcd@v3.3.20+incompatible\clientv3\balancer\resolver\endpoint\endpoint.go:182:31: undefined: resolver.ResolveNowOpt
ion
# github.com/coreos/etcd/clientv3/balancer/picker
..\..\..\pkg\mod\github.com\coreos\etcd@v3.3.20+incompatible\clientv3\balancer\picker\err.go:37:44: undefined: balancer.PickOptions
..\..\..\pkg\mod\github.com\coreos\etcd@v3.3.20+incompatible\clientv3\balancer\picker\roundrobin_balanced.go:55:54: undefined: balancer.PickOptions

解决方法

直接在go.mod文件中添加

replace google.golang.org/grpc => google.golang.org/grpc v1.26.0

再次编译运行即可

E:\goProject\src\github.com\etcd_demo>go run main.go
go: downloading google.golang.org/grpc v1.26.0
go: extracting google.golang.org/grpc v1.26.0
go: finding google.golang.org/grpc v1.26.0
connect to etcd success
q1mi:dsb

上一篇:unrolled/render 集成Masterminds/sprig 增强模版处理


下一篇:【AECC】Adobe After Effects CC软件下载及安装教程(附软件及安装教程)