Basic implementation of Google Cloud's Pub/Sub in Go. Subscriber consumes 5 messages before exiting.
Based on https://cloud.google.com/pubsub/docs/quickstart-client-libraries
Create Project and enable Pub/Sub API for the project.
Install the Google Cloud SDK https://cloud.google.com/sdk/docs/quickstart-macos.
Install the Google Cloud Go Libraries go get -u cloud.google.com/go/pubsub.
Create Server Account under Project with roles: "Pub/Sub Publisher" and "Pub/Sub Subscriber". Generate key and save into keys folder.
Create topic and subscriber
# Create topic
gcloud pubsub topics create my-topic
# Create subscriber and subscribe to topic
gcloud pubsub subscriptions create my-sub --topic my-topicOpen two Terminal windows and export GOOGLE_APPLICATION_CREDENTIALS and PROJECT.
cd go-pubsub
export GOOGLE_APPLICATION_CREDENTIALS=$(pwd)/keys/keys.json
export PROJECT=`gcloud config get-value project`First execute subscriber
go run subscriber/sub.goThen execute publisher
go run publisher/pub.go