mirror of https://github.com/knative/docs.git
fix wrong code sample in helloworld-go README.md (#5746)
This commit is contained in:
parent
68ea6fae2f
commit
304f5c06b1
|
@ -34,9 +34,12 @@ cd knative-docs/code-samples/eventing/helloworld/helloworld-go
|
|||
code creates a basic web server which listens on port 8080:
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
cloudevents "github.com/cloudevents/sdk-go/v2"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
@ -46,23 +49,22 @@ cd knative-docs/code-samples/eventing/helloworld/helloworld-go
|
|||
// In this example we will log the event msg
|
||||
log.Printf("Event received. \n%s\n", event)
|
||||
data := &HelloWorld{}
|
||||
if err: = event.DataAs(data);
|
||||
err != nil {
|
||||
if err := event.DataAs(data); err != nil {
|
||||
log.Printf("Error while extracting cloudevent Data: %s\n", err.Error())
|
||||
return nil, cloudevents.NewHTTPResult(400, "failed to convert data: %s", err)
|
||||
}
|
||||
log.Printf("Hello World Message from received event %q", data.Msg)
|
||||
|
||||
// Respond with another event (optional)
|
||||
// This is optional and is intended to show how to respond back with another event after processing.
|
||||
// The response will go back into the knative eventing system just like any other event
|
||||
newEvent := cloudevents.NewEvent()
|
||||
// Setting the ID here is not necessary. When using NewDefaultClient the ID is set
|
||||
// automatically. We set the ID anyway so it appears in the log.
|
||||
newEvent.SetID(uuid.New().String())
|
||||
newEvent.SetSource("knative/eventing/samples/hello-world")
|
||||
newEvent.SetType("dev.knative.samples.hifromknative")
|
||||
if err: = newEvent.SetData(cloudevents.ApplicationJSON, HiFromKnative {
|
||||
Msg: "Hi from helloworld-go app!"
|
||||
});
|
||||
err != nil {
|
||||
if err := newEvent.SetData(cloudevents.ApplicationJSON, HiFromKnative{Msg: "Hi from helloworld-go app!"}); err != nil {
|
||||
return nil, cloudevents.NewHTTPResult(500, "failed to set response data: %s", err)
|
||||
}
|
||||
log.Printf("Responding with event\n%s\n", newEvent)
|
||||
|
@ -88,13 +90,13 @@ cd knative-docs/code-samples/eventing/helloworld/helloworld-go
|
|||
// HelloWorld defines the Data of CloudEvent with type=dev.knative.samples.helloworld
|
||||
type HelloWorld struct {
|
||||
// Msg holds the message from the event
|
||||
Msg string `json:"msg,omitempty,string"`
|
||||
Msg string `json:"msg,omitempty"`
|
||||
}
|
||||
|
||||
// HiFromKnative defines the Data of CloudEvent with type=dev.knative.samples.hifromknative
|
||||
type HiFromKnative struct {
|
||||
// Msg holds the message from the event
|
||||
Msg string `json:"msg,omitempty,string"`
|
||||
Msg string `json:"msg,omitempty"`
|
||||
}
|
||||
```
|
||||
|
||||
|
|
Loading…
Reference in New Issue