weixin_43832293 2019-09-29 20:49 采纳率: 75%
浏览 519
已采纳

使用go将数据put进etcd报错

package main

import (
"context"
"encoding/json"
"fmt"
"time"

//etcd_client "github.com/coreos/etcd/clientv3"
etcd_client "go.etcd.io/etcd/clientv3"

)

type SecInfoConf struct {
ProductId int
StartTime int
EndTime int
Status int
Total int
Left int
}

const (
//EtcdKey = "/pjy/seckill/product"
EtcdKey = "E:\monster.properties"
)

func SetLogConfToEtcd() {
cli, err := etcd_client.New(etcd_client.Config{
Endpoints: []string{"localhost:2379", "localhost:22379", "localhost:32379"},
DialTimeout: 5 * time.Second,
})
if err != nil {
fmt.Println("in the SetLogConfToEtcd connect is failed err:", err)
return
}
fmt.Println("connect is success")
defer cli.Close()
var secInfoConf []SecInfoConf
secInfoConf = append(
secInfoConf,
SecInfoConf{
ProductId: 1028,
StartTime: 1569686400,
EndTime: 1569772800,
Status: 0,
Total: 1000,
Left: 1000,
},
)
secInfoConf = append(
secInfoConf,
SecInfoConf{
ProductId: 1029,
StartTime: 1569686400,
EndTime: 1569772800,
Status: 0,
Total: 2000,
Left: 2000,
},
)
data, err := json.Marshal(secInfoConf)
if err != nil {
fmt.Println("json Marshal is err: ", err)
return
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
_, err = cli.Put(ctx, EtcdKey, string(data))
cancel()
if err != nil {
fmt.Println("put failed err:", err)
return
}
ctx, cancel = context.WithTimeout(context.Background(), time.Second)
resp, err := cli.Get(ctx, EtcdKey)
cancel()
if err != nil {
fmt.Println("get failed err:", err)
return
}
for _, ev := range resp.Kvs {
fmt.Printf("%v : %v\n", ev.Key, ev.Value)
}
}
func main() {
SetLogConfToEtcd()
}
图片说明

  • 写回答

1条回答 默认 最新

  • 关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错
  • ¥15 换yum源但仍然用不了httpd
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库