Getting Started
Verified against
sdk-goat commit1e99396. Commands assume Go is installed; thev2/go.modmodule declaresgo 1.25.0.
Prerequisites
- Go 1.25 or newer (the
v2module setsgo 1.25.0inv2/go.mod). - A reachable HTTP endpoint to receive events, or the receiver from the second step below.
Install
go get github.com/cloudevents/sdk-go/v2A first working setup
The shortest path that does the core job is to send a CloudEvent over HTTP and receive it. This minimal sender is reduced from samples/http/sender/main.go.
Step 1. Create a sender. WithTimeNow and WithUUIDs fill in the time and id attributes so the event passes validation. The target endpoint is set as http://localhost:8080/ in code.
package main
import (
"context"
"log"
cloudevents "github.com/cloudevents/sdk-go/v2"
)
func main() {
ctx := cloudevents.ContextWithTarget(context.Background(), "http://localhost:8080/")
p, err := cloudevents.NewHTTP()
if err != nil {
log.Fatal(err)
}
c, err := cloudevents.NewClient(p, cloudevents.WithTimeNow(), cloudevents.WithUUIDs())
if err != nil {
log.Fatal(err)
}
e := cloudevents.NewEvent()
e.SetType("com.example.sent")
e.SetSource("example/sender")
_ = e.SetData(cloudevents.ApplicationJSON, map[string]string{"msg": "hello"})
if res := c.Send(ctx, e); cloudevents.IsUndelivered(res) {
log.Printf("failed: %v", res)
}
}Step 2. Run a receiver in a second program to see the event arrive. StartReceiver binds the HTTP listener and dispatches each event to the handler.
package main
import (
"context"
"log"
cloudevents "github.com/cloudevents/sdk-go/v2"
)
func main() {
c, err := cloudevents.NewClientHTTP()
if err != nil {
log.Fatal(err)
}
log.Fatal(c.StartReceiver(context.Background(), func(e cloudevents.Event) {
log.Printf("got event: %s", e)
}))
}Step 3. Start the receiver, then run the sender in another shell.
go run ./receiver
go run ./senderVerify it works
The receiver logs the event it got, including the id, source, type, and the JSON data. On the sender side, cloudevents.IsUndelivered(res) returns false for a delivered event, so no failure line is printed. If the receiver is not running, the sender logs a failed: line with the transport error.
Where to go next
- Other transports: the
v2/protocol/modules cover Kafka, MQTT, AMQP, NATS, and GCP Pub/Sub; see the matching directories undersamples/. - The CloudEvents Core Specification for the full attribute and binding rules.
- The CloudEvents Primer for the design rationale.
- The sdk-go package reference for the full API.