diff --git a/bigquery/bigquery_quickstart/main.go b/bigquery/bigquery_quickstart/main.go new file mode 100644 index 0000000000..304df52d62 --- /dev/null +++ b/bigquery/bigquery_quickstart/main.go @@ -0,0 +1,44 @@ +// Copyright 2016 Google Inc. All rights reserved. +// Use of this source code is governed by the Apache 2.0 +// license that can be found in the LICENSE file. + +// [START bigquery_quickstart] +// Sample bigquery_quickstart creates a Google BigQuery dataset. +package main + +import ( + "fmt" + "golang.org/x/net/context" + "log" + + // Imports the Google Cloud Datastore client package + "cloud.google.com/go/bigquery" +) + +func main() { + ctx := context.Background() + + // Your Google Cloud Platform project ID + projectID := "YOUR_PROJECT_ID" + + // Creates a client + client, err := bigquery.NewClient(ctx, projectID) + if err != nil { + log.Fatalf("Failed to create client: %v", err) + } + + // The name for the new dataset + datasetName := "my_new_dataset" + + // Prepares the new dataset + dataset := client.Dataset(datasetName) + + // Creates the dataset + if err := dataset.Create(ctx); err != nil { + log.Fatalf("Failed to create dataset: %v", err) + } + + fmt.Printf("Dataset %v created", dataset) +} + +// [END bigquery_quickstart] diff --git a/datastore/datastore_quickstart/main.go b/datastore/datastore_quickstart/main.go new file mode 100644 index 0000000000..29b4f2837e --- /dev/null +++ b/datastore/datastore_quickstart/main.go @@ -0,0 +1,51 @@ +// Copyright 2016 Google Inc. All rights reserved. +// Use of this source code is governed by the Apache 2.0 +// license that can be found in the LICENSE file. + +// [START datastore_quickstart] +// Sample datastore_quickstart fetches an entity from Google Cloud Datastore. +package main + +import ( + "fmt" + "golang.org/x/net/context" + "log" + + // Imports the Google Cloud Datastore client package + "cloud.google.com/go/datastore" +) + +type Entity struct { + Value string +} + +func main() { + ctx := context.Background() + + // Your Google Cloud Platform project ID + projectID := "YOUR_PROJECT_ID" + + // Creates a client + client, err := datastore.NewClient(ctx, projectID) + if err != nil { + log.Fatalf("Failed to create client: %v", err) + } + + // The kind of the entity to retrieve + kind := "Person" + // The name/ID of the entity to retrieve + name := "Bob" + // The Datastore key for the entity + key := datastore.NewKey(ctx, kind, name, 0, nil) + + entity := new(Entity) + + // Retrieves the entity + if err := client.Get(ctx, key, entity); err != nil { + log.Fatalf("Failed to get entity: %v", err) + } + + fmt.Printf("Fetched entity: %v", key.String()) +} + +// [END datastore_quickstart] diff --git a/logging/logging_quickstart/main.go b/logging/logging_quickstart/main.go new file mode 100644 index 0000000000..ce438211f7 --- /dev/null +++ b/logging/logging_quickstart/main.go @@ -0,0 +1,51 @@ +// Copyright 2016 Google Inc. All rights reserved. +// Use of this source code is governed by the Apache 2.0 +// license that can be found in the LICENSE file. + +// [START logging_quickstart] +// Sample logging_quickstart writes a log entry to Stackdriver Logging. +package main + +import ( + "fmt" + "golang.org/x/net/context" + "log" + + // Imports the Stackdriver Logging client package + "cloud.google.com/go/logging" +) + +func main() { + ctx := context.Background() + + // Your Google Cloud Platform project ID + projectID := "YOUR_PROJECT_ID" + + // Creates a client + client, err := logging.NewClient(ctx, projectID) + if err != nil { + log.Fatalf("Failed to create client: %v", err) + } + + // The name of the log to write to + logName := "my-log" + + // Selects the log to write to + logger := client.Logger(logName) + + // The data to log + text := "Hello, world!" + + // Adds an entry to the log buffer + logger.Log(logging.Entry{Payload: text}) + + // Closes the client and flushes the buffer to the Stackdriver Logging service + err = client.Close() + if err != nil { + log.Fatalf("Failed to close client: %v", err) + } + + fmt.Printf("Logged: %v", text) +} + +// [END logging_quickstart] diff --git a/pubsub/pubsub_quickstart/main.go b/pubsub/pubsub_quickstart/main.go new file mode 100644 index 0000000000..d2ddae44da --- /dev/null +++ b/pubsub/pubsub_quickstart/main.go @@ -0,0 +1,42 @@ +// Copyright 2016 Google Inc. All rights reserved. +// Use of this source code is governed by the Apache 2.0 +// license that can be found in the LICENSE file. + +// [START pubsub_quickstart] +// Sample pubsub_quickstart creates a Google Cloud Pub/Sub topic. +package main + +import ( + "fmt" + "golang.org/x/net/context" + "log" + + // Imports the Google Cloud Pub/Sub client package + "cloud.google.com/go/pubsub" +) + +func main() { + ctx := context.Background() + + // 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) + } + + // The name for the new topic + topicName := "my-new-topic" + + // Creates the new topic + topic, err := client.CreateTopic(ctx, topicName) + if err != nil { + log.Fatalf("Failed to create topic: %v", err) + } + + fmt.Printf("Topic %v created.", topic) +} + +// [END pubsub_quickstart] diff --git a/storage/storage_quickstart/main.go b/storage/storage_quickstart/main.go new file mode 100644 index 0000000000..2049311ca0 --- /dev/null +++ b/storage/storage_quickstart/main.go @@ -0,0 +1,44 @@ +// Copyright 2016 Google Inc. All rights reserved. +// Use of this source code is governed by the Apache 2.0 +// license that can be found in the LICENSE file. + +// [START storage_quickstart] +// Sample storage_quickstart creates a Google Cloud Storage bucket. +package main + +import ( + "fmt" + "golang.org/x/net/context" + "log" + + // Imports the Google Cloud Storage client package + "cloud.google.com/go/storage" +) + +func main() { + ctx := context.Background() + + // Your Google Cloud Platform project ID + projectID := "YOUR_PROJECT_ID" + + // Creates a client + client, err := storage.NewClient(ctx) + if err != nil { + log.Fatalf("Failed to create client: %v", err) + } + + // The name for the new bucket + bucketName := "my-new-bucket" + + // Prepares a new bucket + bucket := client.Bucket(bucketName) + + // Creates the new bucket + if err := bucket.Create(ctx, projectID, nil); err != nil { + log.Fatalf("Failed to create bucket: %v", err) + } + + fmt.Printf("Bucket %v created.", bucketName) +} + +// [END storage_quickstart] diff --git a/vision/vision_quickstart/main.go b/vision/vision_quickstart/main.go new file mode 100644 index 0000000000..cffbcc1eeb --- /dev/null +++ b/vision/vision_quickstart/main.go @@ -0,0 +1,49 @@ +// Copyright 2016 Google Inc. All rights reserved. +// Use of this source code is governed by the Apache 2.0 +// license that can be found in the LICENSE file. + +// [START vision_quickstart] +// Sample vision_quickstart uses the Google Cloud Vision API to label an image. +package main + +import ( + "fmt" + "golang.org/x/net/context" + "log" + "os" + + // Imports the Google Cloud Vision API client package + "cloud.google.com/go/vision" +) + +func main() { + ctx := context.Background() + + // Creates a client + client, err := vision.NewClient(ctx) + if err != nil { + log.Fatalf("Failed to create client: %v", err) + } + + // The name of the image file to annotate + fileName := "vision/testdata/cat.jpg" + + file, err := os.Open(fileName) + if err != nil { + log.Fatalf("Failed to read file: %v", err) + } + defer file.Close() + image, err := vision.NewImageFromReader(file) + if err != nil { + log.Fatalf("Failed to create image: %v", err) + } + + labels, err := client.DetectLabels(ctx, image, 10) + + fmt.Printf("Labels:\n") + for _, label := range labels { + fmt.Println(label.Description) + } +} + +// [END vision_quickstart]