weixin_43832293 2019-09-29 20:58 采纳率: 75%
浏览 246
已采纳

使用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条回答 默认 最新

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

报告相同问题?

悬赏问题

  • ¥20 绿盟安全扫描--检测到目标站点存在javascript框架库漏洞
  • ¥30 Android STD快速启动
  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包