v1 Create topic quickstart (DEPRECATED)

(DEPRECATED) Create topic quickstart

Code sample

Go

Before trying this sample, follow the Go setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub Go API reference documentation .

To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  // Sample pubsub-quickstart creates a Google Cloud Pub/Sub topic. 
 package 
  
 main 
 import 
  
 ( 
  
 "context" 
  
 "fmt" 
  
 "log" 
  
 "cloud.google.com/go/pubsub" 
 ) 
 func 
  
 main 
 () 
  
 { 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 // Sets your Google Cloud Platform project ID. 
  
 projectID 
  
 := 
  
 "YOUR_PROJECT_ID" 
  
 // Creates a client. 
  
 client 
 , 
  
 err 
  
 := 
  
 pubsub 
 . 
  NewClient 
 
 ( 
 ctx 
 , 
  
 projectID 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 log 
 . 
 Fatalf 
 ( 
 "Failed to create client: %v" 
 , 
  
 err 
 ) 
  
 } 
  
 defer 
  
 client 
 . 
 Close 
 () 
  
 // Sets the id for the new topic. 
  
 topicID 
  
 := 
  
 "my-topic" 
  
 // Creates the new topic. 
  
 topic 
 , 
  
 err 
  
 := 
  
 client 
 . 
 CreateTopic 
 ( 
 ctx 
 , 
  
 topicID 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 log 
 . 
 Fatalf 
 ( 
 "Failed to create topic: %v" 
 , 
  
 err 
 ) 
  
 } 
  
 fmt 
 . 
 Printf 
 ( 
 "Topic %v created.\n" 
 , 
  
 topic 
 ) 
 } 
 

Ruby

Before trying this sample, follow the Ruby setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub Ruby API reference documentation .

To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  # Imports the Google Cloud client library 
 require 
  
 "google/cloud/pubsub" 
 # Instantiates a client 
 pubsub 
  
 = 
  
 Google 
 :: 
 Cloud 
 :: 
  Pubsub 
 
 . 
 new 
 # The name for the new topic 
 # topic_id = "your-topic-id" 
 # Creates the new topic 
 topic 
  
 = 
  
 pubsub 
 . 
 create_topic 
  
 topic_id 
 puts 
  
 "Topic 
 #{ 
 topic 
 . 
 name 
 } 
 created." 
 

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser .

Create a Mobile Website
View Site in Mobile | Classic
Share by: