Skip to content

Commit

Permalink
pubsub/natspubsub: added examples and gathered examples to internal/w…
Browse files Browse the repository at this point in the history
…ebsite/data/examples.json
  • Loading branch information
kucjac committed Jul 21, 2023
1 parent 6e09474 commit 2bd535f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
8 changes: 4 additions & 4 deletions internal/website/data/examples.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 77 additions & 0 deletions pubsub/natspubsub/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"log"

"github.com/nats-io/nats.go"

"gocloud.dev/pubsub"
"gocloud.dev/pubsub/natspubsub"
)
Expand Down Expand Up @@ -131,3 +132,79 @@ func Example_openQueueSubscriptionFromURL() {
}
defer subscription.Shutdown(ctx)
}

func ExampleOpenSubscriptionV2() {
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/natspubsub"
// PRAGMA: On gocloud.dev, hide lines until the next blank line.
ctx := context.Background()
natsConn, err := nats.Connect("nats://nats.example.com")
if err != nil {
log.Fatal(err)
}
defer natsConn.Close()

subscription, err := natspubsub.OpenSubscriptionV2(
natsConn,
"example.mysubject",
nil)
if err != nil {
log.Fatal(err)
}
defer subscription.Shutdown(ctx)
}

func ExampleOpenTopicV2() {
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/natspubsub"
// PRAGMA: On gocloud.dev, hide lines until the next blank line.
ctx := context.Background()

natsConn, err := nats.Connect("nats://nats.example.com")
if err != nil {
log.Fatal(err)
}
defer natsConn.Close()

topic, err := natspubsub.OpenTopicV2(natsConn, "example.mysubject", nil)
if err != nil {
log.Fatal(err)
}
defer topic.Shutdown(ctx)
}

func Example_openTopicV2FromUrl() {
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/natspubsub"
// PRAGMA: On gocloud.dev, hide lines until the next blank line.
ctx := context.Background()

// pubsub.OpenTopic creates a *pubsub.Topic from a URL.
// This URL will Dial the NATS server at the URL in the environment variable
// NATS_SERVER_URL and send messages with subject "example.mysubject".
// This URL will be parsed and the natsv2 attribute will be used to
// use NATS v2.2.0+ native message headers as the message metadata.
topic, err := pubsub.OpenTopic(ctx, "nats://example.mysubject?natsv2")
if err != nil {
log.Fatal(err)
}
defer topic.Shutdown(ctx)
}

func Example_openSubscriptionV2FromURL() {
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/pubsub/natspubsub"
// PRAGMA: On gocloud.dev, hide lines until the next blank line.
ctx := context.Background()

// pubsub.OpenSubscription creates a *pubsub.Subscription from a URL.
// This URL will Dial the NATS server at the URL in the environment variable
// NATS_SERVER_URL and receive messages with subject "example.mysubject".
// This URL will be parsed and the natsv2 attribute will be used to
// use NATS v2.2.0+ native message headers as the message metadata.
subscription, err := pubsub.OpenSubscription(ctx, "nats://example.mysubject?natsv2")
if err != nil {
log.Fatal(err)
}
defer subscription.Shutdown(ctx)
}

1 comment on commit 2bd535f

@challenger14
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coding is a fantastic form of communication. I wish I knew more about how it works.

Please sign in to comment.