Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions bigquery/bigquery_quickstart/main.go
Original file line number Diff line number Diff line change
@@ -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]
54 changes: 54 additions & 0 deletions datastore/datastore_quickstart/main.go
Original file line number Diff line number Diff line change
@@ -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]
54 changes: 54 additions & 0 deletions language/language_quickstart/main.go
Original file line number Diff line number Diff line change
@@ -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]
51 changes: 51 additions & 0 deletions logging/logging_quickstart/main.go
Original file line number Diff line number Diff line change
@@ -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]
78 changes: 78 additions & 0 deletions monitoring/monitoring_quickstart/main.go
Original file line number Diff line number Diff line change
@@ -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]
42 changes: 42 additions & 0 deletions pubsub/pubsub_quickstart/main.go
Original file line number Diff line number Diff line change
@@ -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]
Binary file added speech/speech_quickstart/audio.raw
Binary file not shown.
60 changes: 60 additions & 0 deletions speech/speech_quickstart/main.go
Original file line number Diff line number Diff line change
@@ -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]
Loading