Skip to content

Commit d59162a

Browse files
committed
Add new "quickstart" samples.
1 parent 44a0656 commit d59162a

File tree

8 files changed

+383
-0
lines changed

8 files changed

+383
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2016 Google Inc. All rights reserved.
2+
// Use of this source code is governed by the Apache 2.0
3+
// license that can be found in the LICENSE file.
4+
5+
// [START bigquery_quickstart]
6+
// Sample bigquery_quickstart creates a Google BigQuery dataset.
7+
package main
8+
9+
import (
10+
"fmt"
11+
"golang.org/x/net/context"
12+
"log"
13+
14+
// Imports the Google Cloud Datastore client package
15+
"cloud.google.com/go/bigquery"
16+
)
17+
18+
func main() {
19+
ctx := context.Background()
20+
21+
// Your Google Cloud Platform project ID
22+
projectID := "YOUR_PROJECT_ID"
23+
24+
// Creates a client
25+
client, err := bigquery.NewClient(ctx, projectID)
26+
if err != nil {
27+
log.Fatalf("Failed to create client: %v", err)
28+
}
29+
30+
// The name for the new dataset
31+
datasetName := "my_new_dataset"
32+
33+
// Prepares the new dataset
34+
dataset := client.Dataset(datasetName)
35+
36+
// Creates the dataset
37+
if err := dataset.Create(ctx); err != nil {
38+
log.Fatalf("Failed to create dataset: %v", err)
39+
}
40+
41+
fmt.Printf("Dataset %v created", dataset)
42+
}
43+
44+
// [END bigquery_quickstart]
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2016 Google Inc. All rights reserved.
2+
// Use of this source code is governed by the Apache 2.0
3+
// license that can be found in the LICENSE file.
4+
5+
// [START datastore_quickstart]
6+
// Sample datastore_quickstart fetches an entity from Google Cloud Datastore.
7+
package main
8+
9+
import (
10+
"fmt"
11+
"golang.org/x/net/context"
12+
"log"
13+
14+
// Imports the Google Cloud Datastore client package
15+
"cloud.google.com/go/datastore"
16+
)
17+
18+
type Task struct {
19+
Description string
20+
}
21+
22+
func main() {
23+
ctx := context.Background()
24+
25+
// Your Google Cloud Platform project ID
26+
projectID := "YOUR_PROJECT_ID"
27+
28+
// Creates a client
29+
client, err := datastore.NewClient(ctx, projectID)
30+
if err != nil {
31+
log.Fatalf("Failed to create client: %v", err)
32+
}
33+
34+
// The kind for the new entity
35+
kind := "Task"
36+
// The name/ID for the new entity
37+
name := "sampletask1"
38+
// The Cloud Datastore key for the new entity
39+
taskKey := datastore.NewKey(ctx, kind, name, 0, nil)
40+
41+
// Prepares the new entity
42+
task := new(Task)
43+
task.Description = "Buy milk"
44+
45+
// Saves the entity
46+
if _, err := client.Put(ctx, taskKey, task); err != nil {
47+
log.Fatalf("Failed to save task: %v", err)
48+
}
49+
50+
fmt.Printf("Saved %v: %v", taskKey.String(), task.Description)
51+
}
52+
53+
// [END datastore_quickstart]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2016 Google Inc. All rights reserved.
2+
// Use of this source code is governed by the Apache 2.0
3+
// license that can be found in the LICENSE file.
4+
5+
// [START language_quickstart]
6+
// Sample language_quickstart uses the Google Cloud Natural API to analyze the
7+
// sentiment of "Hello, world!".
8+
package main
9+
10+
import (
11+
"fmt"
12+
"golang.org/x/net/context"
13+
"log"
14+
15+
// Imports the Google Cloud Natural Language API client package
16+
language "cloud.google.com/go/language/apiv1"
17+
languagepb "google.golang.org/genproto/googleapis/cloud/language/v1"
18+
)
19+
20+
func main() {
21+
ctx := context.Background()
22+
23+
// Creates a client
24+
client, err := language.NewClient(ctx)
25+
if err != nil {
26+
log.Fatalf("Failed to create client: %v", err)
27+
}
28+
29+
// The text to analyze
30+
text := "Hello, world!"
31+
32+
// Detects the sentiment of the text
33+
sentiment, err := client.AnalyzeSentiment(ctx, &languagepb.AnalyzeSentimentRequest{
34+
Document: &languagepb.Document{
35+
Source: &languagepb.Document_Content{
36+
Content: text,
37+
},
38+
Type: languagepb.Document_PLAIN_TEXT,
39+
},
40+
EncodingType: languagepb.EncodingType_UTF8,
41+
})
42+
if err != nil {
43+
log.Fatalf("Failed to analyze text: %v", err)
44+
}
45+
46+
fmt.Printf("Text: %v\n", text)
47+
if sentiment.DocumentSentiment.Score >= 0 {
48+
fmt.Printf("Sentiment: positive")
49+
} else {
50+
fmt.Printf("Sentiment: negative")
51+
}
52+
}
53+
54+
// [END language_quickstart]

logging/logging_quickstart/main.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2016 Google Inc. All rights reserved.
2+
// Use of this source code is governed by the Apache 2.0
3+
// license that can be found in the LICENSE file.
4+
5+
// [START logging_quickstart]
6+
// Sample logging_quickstart writes a log entry to Stackdriver Logging.
7+
package main
8+
9+
import (
10+
"fmt"
11+
"golang.org/x/net/context"
12+
"log"
13+
14+
// Imports the Stackdriver Logging client package
15+
"cloud.google.com/go/logging"
16+
)
17+
18+
func main() {
19+
ctx := context.Background()
20+
21+
// Your Google Cloud Platform project ID
22+
projectID := "YOUR_PROJECT_ID"
23+
24+
// Creates a client
25+
client, err := logging.NewClient(ctx, projectID)
26+
if err != nil {
27+
log.Fatalf("Failed to create client: %v", err)
28+
}
29+
30+
// The name of the log to write to
31+
logName := "my-log"
32+
33+
// Selects the log to write to
34+
logger := client.Logger(logName)
35+
36+
// The data to log
37+
text := "Hello, world!"
38+
39+
// Adds an entry to the log buffer
40+
logger.Log(logging.Entry{Payload: text})
41+
42+
// Closes the client and flushes the buffer to the Stackdriver Logging service
43+
err = client.Close()
44+
if err != nil {
45+
log.Fatalf("Failed to close client: %v", err)
46+
}
47+
48+
fmt.Printf("Logged: %v", text)
49+
}
50+
51+
// [END logging_quickstart]

pubsub/pubsub_quickstart/main.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2016 Google Inc. All rights reserved.
2+
// Use of this source code is governed by the Apache 2.0
3+
// license that can be found in the LICENSE file.
4+
5+
// [START pubsub_quickstart]
6+
// Sample pubsub_quickstart creates a Google Cloud Pub/Sub topic.
7+
package main
8+
9+
import (
10+
"fmt"
11+
"golang.org/x/net/context"
12+
"log"
13+
14+
// Imports the Google Cloud Pub/Sub client package
15+
"cloud.google.com/go/pubsub"
16+
)
17+
18+
func main() {
19+
ctx := context.Background()
20+
21+
// Your Google Cloud Platform project ID
22+
projectID := "YOUR_PROJECT_ID"
23+
24+
// Creates a client
25+
client, err := pubsub.NewClient(ctx, projectID)
26+
if err != nil {
27+
log.Fatalf("Failed to create client: %v", err)
28+
}
29+
30+
// The name for the new topic
31+
topicName := "my-new-topic"
32+
33+
// Creates the new topic
34+
topic, err := client.CreateTopic(ctx, topicName)
35+
if err != nil {
36+
log.Fatalf("Failed to create topic: %v", err)
37+
}
38+
39+
fmt.Printf("Topic %v created.", topic)
40+
}
41+
42+
// [END pubsub_quickstart]

storage/storage_quickstart/main.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2016 Google Inc. All rights reserved.
2+
// Use of this source code is governed by the Apache 2.0
3+
// license that can be found in the LICENSE file.
4+
5+
// [START storage_quickstart]
6+
// Sample storage_quickstart creates a Google Cloud Storage bucket.
7+
package main
8+
9+
import (
10+
"fmt"
11+
"golang.org/x/net/context"
12+
"log"
13+
14+
// Imports the Google Cloud Storage client package
15+
"cloud.google.com/go/storage"
16+
)
17+
18+
func main() {
19+
ctx := context.Background()
20+
21+
// Your Google Cloud Platform project ID
22+
projectID := "YOUR_PROJECT_ID"
23+
24+
// Creates a client
25+
client, err := storage.NewClient(ctx)
26+
if err != nil {
27+
log.Fatalf("Failed to create client: %v", err)
28+
}
29+
30+
// The name for the new bucket
31+
bucketName := "my-new-bucket"
32+
33+
// Prepares a new bucket
34+
bucket := client.Bucket(bucketName)
35+
36+
// Creates the new bucket
37+
if err := bucket.Create(ctx, projectID, nil); err != nil {
38+
log.Fatalf("Failed to create bucket: %v", err)
39+
}
40+
41+
fmt.Printf("Bucket %v created.", bucketName)
42+
}
43+
44+
// [END storage_quickstart]
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2016 Google Inc. All rights reserved.
2+
// Use of this source code is governed by the Apache 2.0
3+
// license that can be found in the LICENSE file.
4+
5+
// [START translate_quickstart]
6+
// Sample translate_quickstart translates "Hello, world!" into Russian.
7+
package main
8+
9+
import (
10+
"fmt"
11+
"golang.org/x/net/context"
12+
"golang.org/x/text/language"
13+
"log"
14+
15+
// Imports the Google Cloud Translate client package
16+
"cloud.google.com/go/translate"
17+
)
18+
19+
func main() {
20+
ctx := context.Background()
21+
22+
// Creates a client
23+
client, err := translate.NewClient(ctx)
24+
if err != nil {
25+
log.Fatalf("Failed to create client: %v", err)
26+
}
27+
28+
// The text to translate
29+
text := "Hello, world!"
30+
// The target language
31+
target, err := language.Parse("ru")
32+
if err != nil {
33+
log.Fatalf("Failed to parse target language: %v", err)
34+
}
35+
36+
// Translates some text into Russian
37+
translations, err := client.Translate(ctx, []string{text}, target, nil)
38+
if err != nil {
39+
log.Fatalf("Failed to translate text: %v", err)
40+
}
41+
42+
fmt.Printf("Text: %v\n", text)
43+
fmt.Printf("Translation: %v", translations[0].Text)
44+
}
45+
46+
// [END translate_quickstart]

vision/vision_quickstart/main.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2016 Google Inc. All rights reserved.
2+
// Use of this source code is governed by the Apache 2.0
3+
// license that can be found in the LICENSE file.
4+
5+
// [START vision_quickstart]
6+
// Sample vision_quickstart uses the Google Cloud Vision API to label an image.
7+
package main
8+
9+
import (
10+
"fmt"
11+
"golang.org/x/net/context"
12+
"log"
13+
"os"
14+
15+
// Imports the Google Cloud Vision API client package
16+
"cloud.google.com/go/vision"
17+
)
18+
19+
func main() {
20+
ctx := context.Background()
21+
22+
// Creates a client
23+
client, err := vision.NewClient(ctx)
24+
if err != nil {
25+
log.Fatalf("Failed to create client: %v", err)
26+
}
27+
28+
// The name of the image file to annotate
29+
fileName := "vision/testdata/cat.jpg"
30+
31+
file, err := os.Open(fileName)
32+
if err != nil {
33+
log.Fatalf("Failed to read file: %v", err)
34+
}
35+
defer file.Close()
36+
image, err := vision.NewImageFromReader(file)
37+
if err != nil {
38+
log.Fatalf("Failed to create image: %v", err)
39+
}
40+
41+
labels, err := client.DetectLabels(ctx, image, 10)
42+
43+
fmt.Printf("Labels:\n")
44+
for _, label := range labels {
45+
fmt.Println(label.Description)
46+
}
47+
}
48+
49+
// [END vision_quickstart]

0 commit comments

Comments
 (0)