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,67 +34,69 @@ cd knative-docs/code-samples/eventing/helloworld/helloworld-go
|
||||||
code creates a basic web server which listens on port 8080:
|
code creates a basic web server which listens on port 8080:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"log"
|
"log"
|
||||||
cloudevents "github.com/cloudevents/sdk-go/v2"
|
|
||||||
"github.com/google/uuid"
|
cloudevents "github.com/cloudevents/sdk-go/v2"
|
||||||
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
func receive(ctx context.Context, event cloudevents.Event)( * cloudevents.Event, cloudevents.Result) {
|
func receive(ctx context.Context, event cloudevents.Event) (*cloudevents.Event, cloudevents.Result) {
|
||||||
// Here is where your code to process the event will go.
|
// Here is where your code to process the event will go.
|
||||||
// In this example we will log the event msg
|
// In this example we will log the event msg
|
||||||
log.Printf("Event received. \n%s\n", event)
|
log.Printf("Event received. \n%s\n", event)
|
||||||
data: = & HelloWorld {}
|
data := &HelloWorld{}
|
||||||
if err: = event.DataAs(data);
|
if err := event.DataAs(data); err != nil {
|
||||||
err != nil {
|
log.Printf("Error while extracting cloudevent Data: %s\n", err.Error())
|
||||||
log.Printf("Error while extracting cloudevent Data: %s\n", err.Error())
|
return nil, cloudevents.NewHTTPResult(400, "failed to convert data: %s", err)
|
||||||
return nil, cloudevents.NewHTTPResult(400, "failed to convert data: %s", err)
|
}
|
||||||
}
|
log.Printf("Hello World Message from received event %q", data.Msg)
|
||||||
log.Printf("Hello World Message from received event %q", data.Msg)
|
|
||||||
// Respond with another event (optional)
|
// Respond with another event (optional)
|
||||||
// This is optional and is intended to show how to respond back with another event after processing.
|
// 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
|
// The response will go back into the knative eventing system just like any other event
|
||||||
newEvent: = cloudevents.NewEvent()
|
newEvent := cloudevents.NewEvent()
|
||||||
newEvent.SetID(uuid.New().String())
|
// Setting the ID here is not necessary. When using NewDefaultClient the ID is set
|
||||||
newEvent.SetSource("knative/eventing/samples/hello-world")
|
// automatically. We set the ID anyway so it appears in the log.
|
||||||
newEvent.SetType("dev.knative.samples.hifromknative")
|
newEvent.SetID(uuid.New().String())
|
||||||
if err: = newEvent.SetData(cloudevents.ApplicationJSON, HiFromKnative {
|
newEvent.SetSource("knative/eventing/samples/hello-world")
|
||||||
Msg: "Hi from helloworld-go app!"
|
newEvent.SetType("dev.knative.samples.hifromknative")
|
||||||
});
|
if err := newEvent.SetData(cloudevents.ApplicationJSON, HiFromKnative{Msg: "Hi from helloworld-go app!"}); err != nil {
|
||||||
err != nil {
|
return nil, cloudevents.NewHTTPResult(500, "failed to set response data: %s", err)
|
||||||
return nil, cloudevents.NewHTTPResult(500, "failed to set response data: %s", err)
|
}
|
||||||
}
|
log.Printf("Responding with event\n%s\n", newEvent)
|
||||||
log.Printf("Responding with event\n%s\n", newEvent)
|
return &newEvent, nil
|
||||||
return &newEvent, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
log.Print("Hello world sample started.")
|
log.Print("Hello world sample started.")
|
||||||
c, err: = cloudevents.NewDefaultClient()
|
c, err := cloudevents.NewDefaultClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to create client, %v", err)
|
log.Fatalf("failed to create client, %v", err)
|
||||||
}
|
}
|
||||||
log.Fatal(c.StartReceiver(context.Background(), receive))
|
log.Fatal(c.StartReceiver(context.Background(), receive))
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
1. Create a new file named `eventschemas.go` and paste the following code. This
|
1. Create a new file named `eventschemas.go` and paste the following code. This
|
||||||
defines the data schema of the CloudEvents.
|
defines the data schema of the CloudEvents.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
|
|
||||||
// HelloWorld defines the Data of CloudEvent with type=dev.knative.samples.helloworld
|
// HelloWorld defines the Data of CloudEvent with type=dev.knative.samples.helloworld
|
||||||
type HelloWorld struct {
|
type HelloWorld struct {
|
||||||
// Msg holds the message from the event
|
// 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
|
// HiFromKnative defines the Data of CloudEvent with type=dev.knative.samples.hifromknative
|
||||||
type HiFromKnative struct {
|
type HiFromKnative struct {
|
||||||
// Msg holds the message from the event
|
// Msg holds the message from the event
|
||||||
Msg string `json:"msg,omitempty,string"`
|
Msg string `json:"msg,omitempty"`
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue