Go SDK for CloudEvents
Go to file
Doug Davis 0a006bba61 Fix race condition in kafka tests
also add a hack/until to run the services needed by the integration tests locally

Signed-off-by: Doug Davis <dug@microsoft.com>
2023-10-21 15:45:38 +00:00
.github issue 814 - Add binary content mode for NATS and JetStream protocols 2023-10-19 09:17:43 -04:00
binding/format/protobuf/v2 Update links to cloudevents spec 2023-09-19 07:22:31 -04:00
conformance@6366de11ed Run conformance Cucumber tests (#474) 2020-04-28 07:55:34 -07:00
docs Update links to cloudevents spec 2023-09-19 07:22:31 -04:00
hack Fix race condition in kafka tests 2023-10-21 15:45:38 +00:00
observability Bump golang.org/x/net in /observability/opencensus/v2 2023-10-16 18:57:42 +00:00
protocol issue 814 - Add binary content mode for NATS and JetStream protocols 2023-10-19 09:17:43 -04:00
samples Bump golang.org/x/net from 0.9.0 to 0.17.0 in /samples/pubsub 2023-10-16 15:47:08 +00:00
sql/v2 remove the usage of deprecated io/ioutil package 2023-07-18 06:50:21 +00:00
test Fix race condition in kafka tests 2023-10-21 15:45:38 +00:00
tools bump golang to 1.17 (#778) 2022-07-18 08:27:32 -07:00
v2 Update links to cloudevents spec 2023-09-19 07:22:31 -04: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 Adding github actions for CI (#588) 2020-09-28 09:12:03 -07: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 add link to our security mailing list 2023-10-17 13:00:27 +00: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: Supported go version: 1.17+

Get started

Add the module as dependency using go mod:

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

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)
	}
	log.Fatal(c.StartReceiver(context.Background(), receive));
}

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