在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:cloudevents/sdk-go开源软件地址:https://github.com/cloudevents/sdk-go开源编程语言:Go 98.0%开源软件介绍:CloudEventsGo SDK forOfficial CloudEvents SDK to integrate your application with CloudEvents. This library will help you to:
Note: Supported CloudEvents specification: 0.3, 1.0 Note: Supported go version: 1.14+ Get startedAdd the module as dependency using go mod: go get github.com/cloudevents/sdk-go/[email protected] And import the module in your code import cloudevents "github.com/cloudevents/sdk-go/v2" Send your first CloudEventTo send a CloudEvent using HTTP: func main() {
c, err := cloudevents.NewClientHTTP()
if err != nil {
log.Fatalf("failed to create client, %v", err)
}
// Create an Event.
event := cloudevents.NewEvent()
event.SetSource("example/uri")
event.SetType("example.type")
event.SetData(cloudevents.ApplicationJSON, map[string]string{"hello": "world"})
// Set a target.
ctx := cloudevents.ContextWithTarget(context.Background(), "http://localhost:8080/")
// Send that Event.
if result := c.Send(ctx, event); cloudevents.IsUndelivered(result) {
log.Fatalf("failed to send, %v", result)
}
} Receive your first CloudEventTo start receiving CloudEvents using HTTP: func receive(event cloudevents.Event) {
// do something with event.
fmt.Printf("%s", event)
}
func main() {
// The default client is HTTP.
c, err := cloudevents.NewClientHTTP()
if err != nil {
log.Fatalf("failed to create client, %v", err)
}
log.Fatal(c.StartReceiver(context.Background(), receive));
} Serialize/Deserialize a CloudEventTo marshal a CloudEvent into JSON: event := cloudevents.NewEvent()
event.SetID("example-uuid-32943bac6fea")
event.SetSource("example/uri")
event.SetType("example.type")
event.SetData(cloudevents.ApplicationJSON, map[string]string{"hello": "world"})
bytes, err := json.Marshal(event) To unmarshal JSON back into a CloudEvent: event := cloudevents.NewEvent()
err := json.Unmarshal(bytes, &event) Go further
Community
Each SDK may have its own unique processes, tooling and guidelines, common
governance related material can be found in the
CloudEvents |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论