Files
goLearn/goroutine/task.go
2025-08-15 03:57:05 +08:00

34 lines
709 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package goroutine
import (
"context"
"fmt"
clientv3 "go.etcd.io/etcd/client/v3"
"goLearn/model"
"time"
)
func Runtask(ctx context.Context) {
ctx2 := ctx.Value("value").(model.Task)
fmt.Println(ctx2.Id)
config := clientv3.Config{
Endpoints: []string{"127.0.0.1:2379"}, // etcd节点地址集群模式可填多个
DialTimeout: 5 * time.Second, // 连接超时时间
// 如需认证,添加以下配置
// Username: "root",
// Password: "123456",
}
// 2. 建立连接
client, err := clientv3.New(config)
if err != nil {
fmt.Printf("连接etcd失败: %v\n", err)
return
}
defer client.Close() // 程序退出时关闭连接
fmt.Println("成功连接到etcd")
}