Skip to content

Instantly share code, notes, and snippets.

@DazWilkin
Created November 26, 2017 19:39
Show Gist options
  • Save DazWilkin/bd8300af9f3f9be0954573304775639b to your computer and use it in GitHub Desktop.
Save DazWilkin/bd8300af9f3f9be0954573304775639b to your computer and use it in GitHub Desktop.
package main
import (
"log"
"os"
"cloud.google.com/go/pubsub"
"golang.org/x/net/context"
)
func main() {
ctx := context.Background()
projectID := getEnv("PROJECT_ID")
client, err := pubsub.NewClient(ctx, projectID)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
topicName := getEnv("PUBSUB_TOPIC")
topic, err := client.CreateTopic(ctx, topicName)
if err != nil {
log.Printf("Topic '%v' exists not need to recreate", topicName)
}
msgID, err := topic.Publish(ctx, &pubsub.Message{
Data: []byte("Hello Henry!"),
}).Get(ctx)
if err != nil {
log.Fatalf("Error publishing message: %v", err)
}
log.Printf("Message ID: %v", msgID)
}
func getEnv(k string) string {
v := os.Getenv(k)
if v == "" {
log.Fatalf("Environment variable '%v' required and not set", k)
}
return v
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment