Last active
June 5, 2018 11:25
-
-
Save prantoran/7fdd9073604248fe17eb94c5ff29a561 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func main() { | |
ctx := context.Background() | |
proj := "pubsubx-206305" | |
client, err := pubsub.NewClient(ctx, proj) | |
if err != nil { | |
log.Fatalf("Could not create pubsub Client: %v", err) | |
} | |
const topic = "my-topic" | |
// Create a new topic called my-topic. | |
if err := create(client, topic); err != nil { | |
log.Fatalf("Failed to create a topic: %v", err) | |
} | |
// List all the topics from the project. | |
fmt.Println("Listing all topics from the project:") | |
topics, err := list(client) | |
if err != nil { | |
log.Fatalf("Failed to list topics: %v", err) | |
} | |
for _, t := range topics { | |
fmt.Println(t) | |
} | |
// Publish a text message on the created topic. | |
if err := publish(client, topic, "hello world!"); err != nil { | |
log.Fatalf("Failed to publish: %v", err) | |
} | |
} | |
func list(client *pubsub.Client) ([]*pubsub.Topic, error) { | |
ctx := context.Background() | |
var topics []*pubsub.Topic | |
it := client.Topics(ctx) | |
for { | |
topic, err := it.Next() | |
if err == iterator.Done { | |
break | |
} | |
if err != nil { | |
return nil, err | |
} | |
topics = append(topics, topic) | |
} | |
return topics, nil | |
} | |
func create(client *pubsub.Client, topic string) error { | |
ctx := context.Background() | |
t, err := client.CreateTopic(ctx, topic) | |
if err != nil { | |
return err | |
} | |
fmt.Printf("Topic created: %v\n", t) | |
return nil | |
} | |
func publish(client *pubsub.Client, topic, msg string) error { | |
ctx := context.Background() | |
t := client.Topic(topic) | |
result := t.Publish(ctx, &pubsub.Message{ | |
Data: []byte(msg), | |
}) | |
// Block until the result is returned and a server-generated | |
// ID is returned for the published message. | |
id, err := result.Get(ctx) | |
if err != nil { | |
return err | |
} | |
fmt.Printf("Published a message; msg ID: %v\n", id) | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment