Giter VIP home page Giter VIP logo

golang-samples's Introduction

Google Cloud Platform Go Samples

Kokoro Build Status

This repository holds sample code written in Go that demonstrates the Google Cloud Platform.

Some samples have accompanying guides on cloud.google.com. See respective README files for details.

Google Cloud Samples

To browse ready to use code samples check Google Cloud Samples.

Depending on samples

Copy any code you need from this repository into your own project.

Warning: Do not depend directly on the samples in this repo. Breaking changes may be made at any time without warning.

Contributing changes

Entirely new samples are not accepted. Bug fixes are welcome, either as pull requests or as GitHub issues.

See CONTRIBUTING.md for details on how to contribute.

Licensing

Code in this repository is licensed under the Apache 2.0. See LICENSE.

golang-samples's People

Contributors

aarshd-crest avatar alisskapie avatar angelafunk avatar averikitsch avatar brennaepp avatar broady avatar codyoss avatar daniel-sanche avatar dependabot[bot] avatar enocom avatar frodothetrue avatar grant avatar grayside avatar hengfengli avatar hongalex avatar irataxy avatar jsimonweb avatar kenneth-rosario avatar logicalshark avatar muncus avatar noerog avatar olavloite avatar rahul2393 avatar renovate-bot avatar rsamborski avatar shollyman avatar tbpg avatar telpirion avatar tritone avatar yucentao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

golang-samples's Issues

testing: add tests for MySQL

Bookshelf has a test for MySQL:

    host := os.Getenv("GOLANG_SAMPLES_MYSQL_HOST")
    port := os.Getenv("GOLANG_SAMPLES_MYSQL_PORT")

Either use Cloud SQL or run a local MySQL to test them.

speech: fix API example deadline to 240s, to avoid GRPC deadline too short error

The sample code wasn't working for me due to a GRPC setting error:

2017/04/27 18:23:36 Could not recognize: code:3 message:"Client GRPC deadline too short. Should be at least: 3 * audio-duration + 5 seconds. Current deadline is: 199.99940906430129 second(s). Required at least: 204 second(s)." 

I fixed that by extending the deadline in the context:

        bgctx := context.Background()
	ctx, _ := context.WithDeadline(bgctx, time.Now().Add(240*time.Second))
	conn, err := transport.DialGRPC(ctx,
		option.WithEndpoint("speech.googleapis.com:443"),
		option.WithScopes("https://www.googleapis.com/auth/cloud-platform"),
	)

all: garbage collect GCP resources used in tests

We create GCP resources such as Storage buckets and Pub/Sub topics to run the integration tests. These resources should be deleted if tests fail to clean them.

Build an internal package to generate timestamped resource names and provide a function to garbage collect existing resources if they have been around more than one day.

We can run collection in the beginning of every test or separately once every night once we have scheduled builds.

Spanner Array Examples

Please make some example that includes use of arrays.

I tried 'unmarshalling' the row element type []string into *[]string and []string, but it fails with this:

type []string cannot be used for decoding ARRAY[STRING]

endpoints/getting-started-grpc doesn't work

I've tried running through the instructions, deploying both to GKE and GCE, but in both cases I get the same error when running the client:

$ go run client/main.go -api-key=$API_KEY -addr=$EXTERNAL_IP:80 hi
2017/10/29 14:58:34 could not greet: rpc error: code = Unavailable desc = transport is closing

I'm fairly new to gRPC, so this may not be applicable, but I tried using grpcc to connect, which gives me Error: Trying to connect an http1.x server. I've confirmed that the port specification in container-engine.yaml is correct (-P, not -p).

all: make all snippets tests

Our current approach to test these sample programs is tedious and repetitive. I end up writing the same amount of boring code again and again; first to construct the program and then to test the pieces of the same program.

Rather than writing an actual main package, we should consider the entire repo to be test files that will both illustrate how to use the cloud packages and to assert them.

Since we will tag the reference code with [START *] and [END *], I do think that the tests will be stll readable.

I wanna start doing this when writing snippets for the storage/objects.

/cc @broady

testing: add more e2e tests

Need more support for doing temporary string replacements before deployment. Or adding files to the deployment (e.g., to set up config.go for bookshelf)

pubsub: tests are flaky

Tests don't use a unique topic/subscription name, so they're flaky.

They need to incorporate the project ID in them to ensure they don't conflict with tests run in parallel.

Flood of errors in console when running Datastore sample against local Datastore emulator

I followed the instructions for the Cloud Datastore getting started guide with Go:
https://cloud.google.com/datastore/docs/datastore-api-tutorial

Then I followed the instructions to run the local Datastore emulator:
https://cloud.google.com/datastore/docs/tools/datastore-emulator

Then I restarted my app:
go run tasks.go

The output looks like this (with the connection reset message just continuing indefinitely):
Cloud Datastore Task List

Usage:

new Adds a task with a description
done Marks a task as done
list Lists all tasks by creation time
delete Deletes a task

2016/04/04 21:56:30 transport: http2Client.notifyError got notified that the client transport was broken read tcp 127.0.0.1:51418->127.0.0.1:8687: read: connection reset by peer.
2016/04/04 21:56:30 transport: http2Client.notifyError got notified that the client transport was broken read tcp 127.0.0.1:51419->127.0.0.1:8687: read: connection reset by peer.
2016/04/04 21:56:30 transport: http2Client.notifyError got notified that the client transport was broken read tcp 127.0.0.1:51420->127.0.0.1:8687: read: connection reset by peer.

appengine: NewContext passed an unknown http.Request

If I download and run the appengine_flexible/helloworld app, everything is fine.

If I make the tiniest adjustment and add appengine.NewContext(r) right above fmt.Fprint(w, "Hello world!") then I get a huge panic that results in appengine: NewContext passed an unknown http.Request

Is there something about this that's fundamentally wrong? Please help me, this has been absolutely bedeviling me.

datastore: metadata __namespace__ sample doesn't work?

This one doesn't work for me:
https://github.com/GoogleCloudPlatform/golang-samples/blob/master/datastore/snippets/snippet_test.go#L641-L663

query := datastore.NewQuery("__namespace__").
		Filter("__key__ >=", startNamespace).
		Filter("__key__ <", endNamespace).
		KeysOnly()

I see:
client.GetAll: rpc error: code = InvalidArgument desc = key filter value must be a Key
exit status 1

Looking at the ruby/gql examples, maybe it should be something like

.Filter("__key__ >=", datastore.NameKey("__namespace__", startNamespace, nil)

but I really have no idea.

@broady @rakyll

getting-started/bookshelf: add separate template blocks for title+body

Thank you for the great examples!

Could you add independent titles to the bookshelf app? I.e.

base.html

<title>
  {{ template "title" . }}
</title>

detail.html

{{ define "title" }}
  Detail
{{ end }}

{{ define "body" }}
...
{{ end }}

At the moment that doesn't work because all the included template content is put into "body". I'm having trouble finding an elegant solution with ParseGlob.

Any hints?

[getting-started] Trim the app.yaml down

Across languages, we're trying to trim down the number of flags we include in the default app.yaml. In the case of Managed VMs, we should really only have:

runtime: go
vm: true

Specifically we want to avoid including resources and auto scaling, unless we're specifically showing a sample that works with those settings:

[START resources]

resources:
cpu: .5
memory_gb: 1.3
disk_size_gb: 10

[END resources]

[START scaling]

automatic_scaling:
min_num_instances: 1
max_num_instances: 5
cool_down_period_sec: 60
cpu_utilization:
target_utilization: 0.5

[END scaling]

Flexible vm CloudSQL setup

I'm setting up an Go App Engine flexible environment that can connect to Second Gen Cloud SQL instance.

The method provided in this repo requires authorizations for 0.0.0.0/0 so it's really insecure.

There is suppose to be another method: https://cloud.google.com/sql/docs/app-engine-connect#gaev2-csqlv2
However, it leads to an unknown network cloudsql or if the cloudsql dialer is added cloudsql: not supported in App Engine "flexible environment" error. (here is the reason for the second error https://github.com/golang/appengine/blob/master/cloudsql/cloudsql_vm.go)

Is the document on the cloud.google.com page suppose to work? If so, could the example in this repo be updated to show how to do this?

@broady

[getting-started/bookshelf] Problem saving ImageURLs

Hello,

When following the tutorial, I'm unable to display images.
I'm using mongo for the DB, so I took a look at a record I created:

{ "_id" : ObjectId("565a09fbbc16007f3ce3bed6"), "id" : NumberLong("6011543483995065438"), "title" : "Hmm", "author"
 : "", "publisheddate" : "", "imageurl" : "https://storage.googleapis.com/&{%!s(*storage.ACLHandle=&{0xc8201d42c0 g
o-cloud-engine-tutorial  false}) %!s(*storage.ACLHandle=&{0xc8201d42c0 go-cloud-engine-tutorial  true}) %!s(*storag
e.Client=&{0xc8201dc240 0xc8201d6180}) go-cloud-engine-tutorial}/73dd02f2-5a76-4a6b-b4ed-787df5502cfc.png", "descri
ption" : "", "createdby" : "", "createdbyid" : "anonymous" }

It seems that uploadFileFromForm is using the go object print notation for bookshelf.StorageBucket which results in an invalid URL.

Error installing google cloud storage SDK

I get the following error when trying to install google's SDK For their Cloud Storage product.

go get -u cloud.google.com/go/storage
# cloud.google.com/go/storage
../../../../cloud.google.com/go/storage/copy.go:81: cannot use res.TotalBytesRewritten (type int64) as type uint64 in argument to c.ProgressFunc
../../../../cloud.google.com/go/storage/copy.go:81: cannot use res.ObjectSize (type int64) as type uint64 in argument to c.ProgressFunc

spanner from standard gae

what about use new spanner database from standard appengine (golang)

Is it a good practice and recommended?

Application no execute

Hello i'm problem in run main.go.
Go version 1.9.2

go get -u cloud.google.com/go/...
go get -u cloud.google.com/go/speech/apiv1

go run main.go

Result:

../../cloud.google.com/go/longrunning/autogen/operations_client.go:26:2: cannot find package "google.golang.org/api/iterator" in any of:
        /usr/local/go/src/google.golang.org/api/iterator (from $GOROOT)
        /home/jefferson/Projects/GO/src/google.golang.org/api/iterator (from $GOPATH)
../../cloud.google.com/go/longrunning/autogen/operations_client.go:27:2: cannot find package "google.golang.org/api/option" in any of:
        /usr/local/go/src/google.golang.org/api/option (from $GOROOT)
        /home/jefferson/Projects/GO/src/google.golang.org/api/option (from $GOPATH)
../../cloud.google.com/go/longrunning/autogen/operations_client.go:28:2: cannot find package "google.golang.org/api/transport" in any of:
        /usr/local/go/src/google.golang.org/api/transport (from $GOROOT)
        /home/jefferson/Projects/GO/src/google.golang.org/api/transport (from $GOPATH)

Speech Async sample not working

[daharon-macpro:captionasync daharon$ go run captionasync.go
captionasync.go:17:2: cannot find package "github.com/gogo/protobuf/proto" in any of:
/usr/local/go/src/github.com/gogo/protobuf/proto (from $GOROOT)
/Users/daharon/go/src/github.com/gogo/protobuf/proto (from $GOPATH)

testing: add a tests target without credentials

Add a test target that doesn't have access to the credentials (and with GOLANG_SAMPLES_PROJECT unset) to make sure SystemTest is called appropriately and that contributors can run the tests.

It doesn't really matter which Go version it runs under.

/cc @rakyll

Monitoring permission issue

The example of "listresources.go" runs fine.
I try metric "logging.googleapis.com/log_entry_count" and it works fine.
Problem is that I change the metric to "pubsub.googleapi.com/subscription/***", and I got the permission error
Error 403: User does not have permission to see metric pubsub.googleapi.com/subscription/byte_cost, forbidden

Any idea? Thx.

Statically built Client Library sample does not work when containerized

Perplexed but anxious that this is a trivial or obvious solution. The working Docker container (#3) is 1GB and expensive to move around. If it worked, it would be more convenient, to use Docker container (#2) as this is only 12MB.

  1. Statically built sample -- works
  2. Containerize #1 -- doesn't work (but only 11.9MB) -- blocks (indefinitely)
  3. Containerize using golang|go-wrapper -- works (but 1.02GB!)

Took this Golang sample:
https://github.com/GoogleCloudPlatform/golang-samples/blob/master/pubsub/pubsub_quickstart/main.go
https://cloud.google.com/pubsub/docs/reference/libraries

Extended it trivially to (a) reference environment variables; (b) publish a message too:
https://gist.github.com/DazWilkin/bd8300af9f3f9be0954573304775639b

1. Static build no container

CGO_ENABLED=0 \
GOOS=linux \
GOARCH=amd64 \
go build -ldflags '-extldflags "-static"' -o pubsub pubsub.go

Then:

PROJECT_ID=${PROJECT} \
PUBSUB_TOPIC=${PUB} \
GOOGLE_APPLICATION_CREDENTIALS=./key.json \
./pubsub

and:

2017/11/26 11:45:25 Topic '${PUB}' exists no need to recreate
2017/11/26 11:45:26 Message ID: 175704661496913

2. Containerize it

Dockerfile.scratch

FROM scratch
ADD pubsub /
ENTRYPOINT ["/pubsub"]

and:

docker build \
--tag=${IMAGE}:${TAG} \
--file=Dockerfile.scratch \
.

docker run \
--interactive \
--tty \
--rm \
--env=PROJECT_ID=$PROJECT \
--env=PUBSUB_TOPIC=$PUB \
--env=GOOGLE_APPLICATION_CREDENTIALS=/config/key.json \
--volume=$PWD:/config \
$IMAGE:$TAG

and appears to just block indefinitely on pubsub.NewClient(...)

3. Containerize using golang|go-wrapper

Dockerfile.golang

FROM golang:1.9

WORKDIR /go/src/app
COPY . .

RUN go-wrapper download   # "go get -d -v ./..."
RUN go-wrapper install    # "go install -v ./..."

CMD ["go-wrapper", "run"] # ["app"]

and build using the above and same run:

docker build \
--tag=${IMAGE}:${TAG} \
--file=Dockerfile.golang \
.

docker run \
--interactive \
--tty \
--rm \
--env=PROJECT_ID=$PROJECT \
--env=PUBSUB_TOPIC=$PUB \
--env=GOOGLE_APPLICATION_CREDENTIALS=/config/key.json \
--volume=$PWD:/config \
$IMAGE:$TAG

Generates:

+ exec app
2017/11/26 19:53:19 Topic $PUB' exists no need to recreate
2017/11/26 19:53:19 Message ID: 175706135054686

Add READMEs for speech

  1. Readme should contain instructions that explain how to get the sample to compile and how to run it.
  2. Readme should include the "go get -u cloud.google.com/go/speech/apiv1" command so that users will know that they need to run it

getting-started/bookshelf: mysql reconnects not handled

The sample runs "use library" only once when the app starts. If the sql package at any point returns a new connection, the app won't work properly.

I think the fix is to call "sql.Open" again after verifying that the DB exists, specifying the DB name in the DSN.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.