diff --git a/bigquery/bigquery_quickstart/main.go b/bigquery/bigquery_quickstart/main.go new file mode 100644 index 0000000000..3de3800a5d --- /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" + "log" + + // Imports the Google Cloud BigQuery client package. + "cloud.google.com/go/bigquery" + "golang.org/x/net/context" +) + +func main() { + ctx := context.Background() + + // Sets 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) + } + + // Sets the name for the new dataset. + datasetName := "my_new_dataset" + + // Creates a Dataset instance. + dataset := client.Dataset(datasetName) + + // Creates the new BigQuery dataset. + if err := dataset.Create(ctx); err != nil { + log.Fatalf("Failed to create dataset: %v", err) + } + + fmt.Printf("Dataset created\n") +} + +// [END bigquery_quickstart] diff --git a/datastore/datastore_quickstart/main.go b/datastore/datastore_quickstart/main.go new file mode 100644 index 0000000000..8cda99c230 --- /dev/null +++ b/datastore/datastore_quickstart/main.go @@ -0,0 +1,54 @@ +// 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" + "log" + + // Imports the Google Cloud Datastore client package. + "cloud.google.com/go/datastore" + "golang.org/x/net/context" +) + +type Task struct { + Description string +} + +func main() { + ctx := context.Background() + + // Setx 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) + } + + // Sets the kind for the new entity. + kind := "Task" + // Sets the name/ID for the new entity. + name := "sampletask1" + // Creates a Key instance. + taskKey := datastore.NameKey(kind, name, nil) + + // Creates a Task instance. + task := Task{ + Description: "Buy milk", + } + + // Saves the new entity. + if _, err := client.Put(ctx, taskKey, &task); err != nil { + log.Fatalf("Failed to save task: %v", err) + } + + fmt.Printf("Saved %v: %v\n", taskKey, task.Description) +} + +// [END datastore_quickstart] diff --git a/language/language_quickstart/main.go b/language/language_quickstart/main.go new file mode 100644 index 0000000000..18156d393d --- /dev/null +++ b/language/language_quickstart/main.go @@ -0,0 +1,54 @@ +// 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 language_quickstart] +// Sample language-quickstart uses the Google Cloud Natural API to analyze the +// sentiment of "Hello, world!". +package main + +import ( + "fmt" + "log" + + // Imports the Google Cloud Natural Language API client package. + language "cloud.google.com/go/language/apiv1" + "golang.org/x/net/context" + languagepb "google.golang.org/genproto/googleapis/cloud/language/v1" +) + +func main() { + ctx := context.Background() + + // Creates a client. + client, err := language.NewClient(ctx) + if err != nil { + log.Fatalf("Failed to create client: %v", err) + } + + // Sets the text to analyze. + text := "Hello, world!" + + // Detects the sentiment of the text. + sentiment, err := client.AnalyzeSentiment(ctx, &languagepb.AnalyzeSentimentRequest{ + Document: &languagepb.Document{ + Source: &languagepb.Document_Content{ + Content: text, + }, + Type: languagepb.Document_PLAIN_TEXT, + }, + EncodingType: languagepb.EncodingType_UTF8, + }) + if err != nil { + log.Fatalf("Failed to analyze text: %v", err) + } + + fmt.Printf("Text: %v\n", text) + if sentiment.DocumentSentiment.Score >= 0 { + fmt.Println("Sentiment: positive") + } else { + fmt.Println("Sentiment: negative") + } +} + +// [END language_quickstart] diff --git a/logging/logging_quickstart/main.go b/logging/logging_quickstart/main.go new file mode 100644 index 0000000000..96ddcba0dc --- /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" + "log" + + // Imports the Stackdriver Logging client package. + "cloud.google.com/go/logging" + "golang.org/x/net/context" +) + +func main() { + ctx := context.Background() + + // Sets 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) + } + + // Sets the name of the log to write to. + logName := "my-log" + + // Selects the log to write to. + logger := client.Logger(logName) + + // Sets 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. + if err := client.Close(); err != nil { + log.Fatalf("Failed to close client: %v", err) + } + + fmt.Printf("Logged: %v\n", text) +} + +// [END logging_quickstart] diff --git a/monitoring/monitoring_quickstart/main.go b/monitoring/monitoring_quickstart/main.go new file mode 100644 index 0000000000..5ff4df72dd --- /dev/null +++ b/monitoring/monitoring_quickstart/main.go @@ -0,0 +1,78 @@ +// Copyright 2017 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 monitoring_quickstart] +// Sample monitoring-quickstart writes a data point to Stackdriver Monitoring. +package main + +import ( + "fmt" + "log" + "time" + + // Imports the Stackdriver Monitoring client package. + monitoring "cloud.google.com/go/monitoring/apiv3" + googlepb "github.com/golang/protobuf/ptypes/timestamp" + "golang.org/x/net/context" + metricpb "google.golang.org/genproto/googleapis/api/metric" + monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres" + monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3" +) + +func main() { + ctx := context.Background() + + // Creates a client. + client, err := monitoring.NewMetricClient(ctx) + if err != nil { + log.Fatalf("Failed to create client: %v", err) + } + + // Sets your Google Cloud Platform project ID. + projectID := "YOUR_PROJECT_ID" + + // Prepares an individual data point + dataPoint := &monitoringpb.Point{ + Interval: &monitoringpb.TimeInterval{ + EndTime: &googlepb.Timestamp{ + Seconds: time.Now().Unix(), + }, + }, + Value: &monitoringpb.TypedValue{ + Value: &monitoringpb.TypedValue_DoubleValue{ + DoubleValue: 123.45, + }, + }, + } + + // Writes time series data. + if err := client.CreateTimeSeries(ctx, &monitoringpb.CreateTimeSeriesRequest{ + Name: monitoring.MetricProjectPath(projectID), + TimeSeries: []*monitoringpb.TimeSeries{ + { + Metric: &metricpb.Metric{ + Type: "custom.googleapis.com/stores/daily_sales", + Labels: map[string]string{ + "store_id": "Pittsburg", + }, + }, + Resource: &monitoredrespb.MonitoredResource{ + Type: "global", + Labels: map[string]string{ + "project_id": projectID, + }, + }, + Points: []*monitoringpb.Point{ + dataPoint, + }, + }, + }, + }); err != nil { + log.Fatalf("Failed to write time series data: %v", err) + } + + fmt.Printf("Done writing time series data.\n") +} + +// [END monitoring_quickstart] diff --git a/pubsub/pubsub_quickstart/main.go b/pubsub/pubsub_quickstart/main.go new file mode 100644 index 0000000000..161ff83b3b --- /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" + "log" + + // Imports the Google Cloud Pub/Sub client package. + "cloud.google.com/go/pubsub" + "golang.org/x/net/context" +) + +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) + } + + // Sets 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.\n", topic) +} + +// [END pubsub_quickstart] diff --git a/speech/speech_quickstart/audio.raw b/speech/speech_quickstart/audio.raw new file mode 100644 index 0000000000..5ebf79d3c9 Binary files /dev/null and b/speech/speech_quickstart/audio.raw differ diff --git a/speech/speech_quickstart/main.go b/speech/speech_quickstart/main.go new file mode 100644 index 0000000000..d099876cf6 --- /dev/null +++ b/speech/speech_quickstart/main.go @@ -0,0 +1,60 @@ +// Copyright 2017 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 speech_quickstart] +// Sample speech-quickstart uses the Google Cloud Speech API to transcribe +// audio. +package main + +import ( + "fmt" + "io/ioutil" + "log" + + // Imports the Google Cloud Speech API client package. + "golang.org/x/net/context" + + speech "cloud.google.com/go/speech/apiv1" + speechpb "google.golang.org/genproto/googleapis/cloud/speech/v1" +) + +func main() { + ctx := context.Background() + + // Creates a client. + client, err := speech.NewClient(ctx) + if err != nil { + log.Fatalf("Failed to create client: %v", err) + } + + // Sets the name of the audio file to transcribe. + filename := "/path/to/audio.raw" + + // Reads the audio file into memory. + data, err := ioutil.ReadFile(filename) + if err != nil { + log.Fatalf("Failed to read file: %v", err) + } + + // Detects speech in the audio file. + resp, err := client.Recognize(ctx, &speechpb.RecognizeRequest{ + Config: &speechpb.RecognitionConfig{ + Encoding: speechpb.RecognitionConfig_LINEAR16, + SampleRateHertz: 16000, + LanguageCode: "en-US", + }, + Audio: &speechpb.RecognitionAudio{ + AudioSource: &speechpb.RecognitionAudio_Content{Content: data}, + }, + }) + + // Prints the results. + for _, result := range resp.Results { + for _, alt := range result.Alternatives { + fmt.Printf("\"%v\" (confidence=%3f)\n", alt.Transcript, alt.Confidence) + } + } +} + +// [END speech_quickstart] diff --git a/storage/storage_quickstart/main.go b/storage/storage_quickstart/main.go new file mode 100644 index 0000000000..e11dbd9f96 --- /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" + "log" + + // Imports the Google Cloud Storage client package. + "cloud.google.com/go/storage" + "golang.org/x/net/context" +) + +func main() { + ctx := context.Background() + + // Sets 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) + } + + // Sets the name for the new bucket. + bucketName := "my-new-bucket" + + // Creates a Bucket instance. + 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.\n", bucketName) +} + +// [END storage_quickstart] diff --git a/translate/translate_quickstart/main.go b/translate/translate_quickstart/main.go new file mode 100644 index 0000000000..b2d0a6cc05 --- /dev/null +++ b/translate/translate_quickstart/main.go @@ -0,0 +1,46 @@ +// 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 translate_quickstart] +// Sample translate-quickstart translates "Hello, world!" into Russian. +package main + +import ( + "fmt" + "log" + + // Imports the Google Cloud Translate client package. + "cloud.google.com/go/translate" + "golang.org/x/net/context" + "golang.org/x/text/language" +) + +func main() { + ctx := context.Background() + + // Creates a client. + client, err := translate.NewClient(ctx) + if err != nil { + log.Fatalf("Failed to create client: %v", err) + } + + // Sets the text to translate. + text := "Hello, world!" + // Sets the target language. + target, err := language.Parse("ru") + if err != nil { + log.Fatalf("Failed to parse target language: %v", err) + } + + // Translates the text into Russian. + translations, err := client.Translate(ctx, []string{text}, target, nil) + if err != nil { + log.Fatalf("Failed to translate text: %v", err) + } + + fmt.Printf("Text: %v\n", text) + fmt.Printf("Translation: %v\n", translations[0].Text) +} + +// [END translate_quickstart] diff --git a/vision/vision_quickstart/main.go b/vision/vision_quickstart/main.go new file mode 100644 index 0000000000..12f12c7f2e --- /dev/null +++ b/vision/vision_quickstart/main.go @@ -0,0 +1,52 @@ +// 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" + "log" + "os" + + // Imports the Google Cloud Vision API client package. + "cloud.google.com/go/vision" + "golang.org/x/net/context" +) + +func main() { + ctx := context.Background() + + // Creates a client. + client, err := vision.NewClient(ctx) + if err != nil { + log.Fatalf("Failed to create client: %v", err) + } + + // Sets 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) + if err != nil { + log.Fatalf("Failed to detect labels: %v", err) + } + + fmt.Println("Labels:") + for _, label := range labels { + fmt.Println(label.Description) + } +} + +// [END vision_quickstart]