Go SDK for CloudEvents
Go to file
Doug Davis a823fb4ac7
Merge pull request #1167 from cloudevents/automated-dependency-updates
chore: update dependencies
2025-06-23 00:17:31 -04:00
.github fix: update golangci-lint version 2025-05-12 10:05:07 +02:00
binding/format/protobuf/v2 chore: update dependencies 2025-06-16 03:33:59 +00:00
conformance@6366de11ed Run conformance Cucumber tests (#474) 2020-04-28 07:55:34 -07:00
docs chore(deps-dev): Bump nokogiri 2025-04-22 04:51:22 +00:00
hack chore: update dependencies incl. tests 2025-05-12 10:05:07 +02:00
observability chore: update dependencies 2025-06-16 03:33:59 +00:00
protocol chore: update dependencies 2025-06-23 03:35:07 +00:00
samples chore: update dependencies 2025-06-23 03:35:07 +00:00
sql/v2 chore: update dependencies 2025-06-16 03:33:59 +00:00
test chore: update dependencies 2025-06-23 03:35:07 +00:00
tools chore: update go deps 2025-04-20 08:59:19 +02:00
v2 fix: simplify isJSON 2025-06-12 17:55:05 +10:00
.gitignore mqtt protocol binding (#910) 2023-07-13 09:08:08 -04:00
.gitmodules Run conformance Cucumber tests (#474) 2020-04-28 07:55:34 -07:00
.golangci.yaml upgrade-lint 2025-03-25 18:30:51 +00:00
CONTRIBUTING.md docs: add contributor, maintainer and pr guides (#501) 2020-05-11 13:29:47 -07:00
LICENSE Initial commit 2018-06-25 11:31:52 -07:00
MAINTAINERS.md Governance docs per CE PR 1226 2023-09-21 17:07:27 +00:00
README.md chore: replace Dependabot with custom script 2025-04-20 09:10:27 +02:00
RELEASING.md Update RELEASING to be more explicit 2023-04-11 13:26:11 +00:00
maintainer_guidelines.md migrate master to main branch name (#672) 2021-04-08 16:08:11 -07:00
pr_guidelines.md migrate master to main branch name (#672) 2021-04-08 16:08:11 -07:00

README.md

Go SDK for CloudEvents

go-doc Go Report Card Releases LICENSE

Official CloudEvents SDK to integrate your application with CloudEvents.

This library will help you to:

Note: Supported CloudEvents specification: 0.3, 1.0

Note: Tested and supported go version(s): 1.23+

Get started

Add the module as dependency to your project:

go get github.com/cloudevents/sdk-go/v2

And import the module in your code

import cloudevents "github.com/cloudevents/sdk-go/v2"

Send your first CloudEvent

To 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)
	} else {
		log.Printf("sent: %v", event)
		log.Printf("result: %v", result)
	}
}

Receive your first CloudEvent

To 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)
	}
	if err = c.StartReceiver(context.Background(), receive); err != nil {
		log.Fatalf("failed to start receiver: %v", err)
	}
}

Create a CloudEvent from an HTTP Request

func handler(w http.ResponseWriter, r *http.Request) {
	event, err := cloudevents.NewEventFromHTTPRequest(r)
	if err != nil {
		log.Printf("failed to parse CloudEvent from request: %v", err)
		http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
	}
	w.Write([]byte(event.String()))
	log.Println(event.String())
}

Serialize/Deserialize a CloudEvent

To 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 community directory. In particular, in there you will find information concerning how SDK projects are managed, guidelines for how PR reviews and approval, and our Code of Conduct information.

If there is a security concern with one of the CloudEvents specifications, or with one of the project's SDKs, please send an email to cncf-cloudevents-security@lists.cncf.io.

Additional SDK Resources