Skip to content

Commit

Permalink
Add new "quickstart" samples.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed Oct 5, 2016
1 parent b5f7a02 commit baf8308
Show file tree
Hide file tree
Showing 6 changed files with 281 additions and 0 deletions.
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"
"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]
51 changes: 51 additions & 0 deletions datastore/datastore_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 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]
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"
"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]
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"
"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]
44 changes: 44 additions & 0 deletions storage/storage_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 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]
49 changes: 49 additions & 0 deletions vision/vision_quickstart/main.go
Original file line number Diff line number Diff line change
@@ -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]

0 comments on commit baf8308

Please sign in to comment.