golang-samples Documentation

Sample apps and code written for Google Cloud in the Go programming language.

RAW Doc

Technical Documentation: GoogleCloudPlatform/golang-samples

ℹ️ Provenance: Multi-Tier Ingestion · CodeWiki Reference

Overview (README.md)

Google Cloud Platform Go Samples

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

Interested in contributing a new sample? Please open an issue describing the intended sample. Additionally, 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.


Appengine Backends Index (`docs/appengine/backends/index.txt`)

// NOTE: Backends are deprecated. The google.golang.org/appengine package does not support backends.

// [START shutdown]
func init() {
http.HandleFunc("/_ah/stop", shutdownHandler)
}

func shutdownHandler(w http.ResponseWriter, r *http.Request) {
doSomeWork()
saveState()
}
// [END shutdown]

// [START addressing_backends]
func handler(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)

text
// Get the details of the current backend.
name, index := appengine.BackendInstance(ctx)

// Get the hostname of this specific backend.
hostname := appengine.BackendHostname(ctx, name, index)

fmt.Fprint(w, "I am backend %s instance %d ", name, index)
fmt.Fprint(w, "serving http://%s", hostname)

}
// [END addressing_backends]


Appengine Config Appconfig (`docs/appengine/config/appconfig.txt`)

// [START gae_golang_about_app_yaml_example]
runtime: go
api_version: go1

handlers:

  • url: /stylesheets
    static_dir: stylesheets

  • url: /(..(gif|png|jpg))$
    static_files: static/\1
    upload: static/.
    .(gif|png|jpg)$

  • url: /.*
    script: _go_app
    // [END gae_golang_about_app_yaml_example]

// [START gae_golang_handlers_example]
handlers:

  • url: /images
    static_dir: static/images

  • url: /.*
    script: _go_app
    // [END gae_golang_handlers_example]

// [START gae_golang_script_example]
handlers:

The root URL (/) is handled by the Go application.

No other URLs match this pattern.

  • url: /
    script: _go_app

The URL /index.html is also handled by the Go application.

  • url: /index.html
    script: _go_app

A regular expression indicating that it should be handled

by the Go application.

  • url: /browse/(books|videos|tools)
    script: _go_app

All other URLs are handled by the Go application.

  • url: /.*
    script: _go_app
    // [END gae_golang_script_example]

// [START gae_golang_login_example]
handlers:

  • url: /profile/.*
    script: _go_app
    login: required

  • url: /admin/.*
    script: _go_app
    login: admin

  • url: /.*
    script: _go_app
    // [END gae_golang_login_example]

//[START gae_golang_auth_fail_action_example]
handlers:

  • url: /secure_api/.*
    script: _go_app
    login: required
    auth_fail_action: unauthorized
    //[END gae_golang_auth_fail_action_example]

//[START gae_golang_expiration_example]
runtime: go
api_version: go1

default_expiration: "4d 5h"

handlers:

...

//[END gae_golang_expiration_example]

//[START gae_golang_secure_urls]
handlers:

  • url: /youraccount/.*
    script: _go_app
    login: required
    secure: always
    //[END gae_golang_secure_urls]

//[START gae_golang_inbound_services]
inbound_services:

  • warmup
    //[END gae_golang_inbound_services]

//[START gae_golang_os_getenv_example]
import "os"
//...
if v := os.Getenv("MY_VAR"); v != "" {
//...
}
//[END gae_golang_os_getenv_example]

//[START gae_golang_redirect_http_response_code]
handlers:

  • url: /youraccount/.*
    script: _go_app
    login: required
    secure: always
    redirect_http_response_code: 301
    //[END gae_golang_redirect_http_response_code]

Appengine Config Cron (`docs/appengine/config/cron.txt`)

// [START securing_URLs_for_cron]
runtime: go
api_version: go1

handlers:

  • url: /report/weekly
    script: _go_app
    login: admin
    // [END securing_URLs_for_cron]

Appengine Googlecloudstorageclient Download (`docs/appengine/googlecloudstorageclient/download.txt`)

// [START GOPATH_setup]
mkdir -p $HOME/go
export GOPATH=$HOME/go
// [END GOPATH_setup]
// [START Go_install]
go get -u golang.org/x/oauth2
go get -u cloud.google.com/go/storage
go get -u google.golang.org/appengine/...
// [END Go_install]


Appengine Googlecloudstorageclient Getstarted (`docs/appengine/googlecloudstorageclient/getstarted.txt`)

// [START App_Engine_Example]
/path/to/AppEngSDK/dev_appserver.py . --appidentity_email_address @developer.gserviceaccount.com --appidentity_private_key_path pem_file.pem
// [END App_Engine_Example]
// [START Managed_VM_Example]
/path/to/CloudSDK/gcloud app run ./app.yaml --appidentity-email-address @developer.gserviceaccount.com --appidentity-private-key-path pem_file.pem
// [END Managed_VM_Example]


Appengine Logs Index (`docs/appengine/logs/index.txt`)

// [START the_development_server_and_Logs_API_1]
dev_appserver.py --persist_logs your-app-directory
// [END the_development_server_and_Logs_API_1]

// [START the_development_server_and_Logs_API_2]
dev_appserver.py --logs_path=your-path/your-logfile-name your-app-directory
// [END the_development_server_and_Logs_API_2]


Appengine Modules Converting (`docs/appengine/modules/converting.txt`)

// [START sample_1]
backends:

  • name: memdb
    class: B8
    instances: 5
  • name: worker
    start: _go_app
    options: failfast
  • name: cmdline
    options: dynamic
    // [END sample_1]

// [START sample_2]
service: memdb
instance_class: B8
manual_scaling:
instances: 5
// [END sample_2]

// [START sample_3]
service: worker

For failfast functionality, please use the ‘X-AppEngine-FailFast’ header on requests made to this service.

manual_scaling:
instances: 1
handlers:

If a service has an /_ah/start handler, it should be listed first.

  • url: /_ah/start
    script: _go_app
    // [END sample_3]

// [START sample_4]
service: cmdline
basic_scaling:
max_instances: 1
// [END sample_4]

// [START sample_5]
$ path-to-sdk/backends_conversion.py backends.yaml app.yaml
// [END sample_5]


Appengine Modules Index (`docs/appengine/modules/index.txt`)

// [START manual_scaling]
service: my_service
runtime: go
api_version: go1
instance_class: B8
manual_scaling:
instances: 5
// [END manual_scaling]

// [START basic_scaling]
service: my_service
runtime: go
api_version: go1
instance_class: B8
basic_scaling:
max_instances: 11
idle_timeout: 10m
// [END basic_scaling]

// [START automatic_scaling]
service: my_service
runtime: go
api_version: go1
instance_class: F2
automatic_scaling:
min_idle_instances: 5
max_idle_instances: automatic # default value
min_pending_latency: 30ms # default value
max_pending_latency: automatic
max_concurrent_requests: 50
// [END automatic_scaling]

// [START example_1]
runtime: go
api_version: go1
// [END example_1]

// [START example_2]
service: mobile-frontend
runtime: go
api_version: go1

automatic_scaling:
min_idle_instances: 2
max_pending_latency: 1s
// [END example_2]

// [START example_3]
service: my_service
runtime: go
api_version: go1

instance_class: B8
manual_scaling:
instances: 1
// [END example_3]


Appengine Modules Routing (`docs/appengine/modules/routing.txt`)

// [START sample_1]
dispatch:

Default service serves the typical web resources and all static resources.

  • url: "*/favicon.ico"
    service: default

Default service serves simple hostname request.

  • url: "simple-sample.appspot.com/"
    service: default

Send all mobile traffic to the mobile frontend.

  • url: "/mobile/"
    service: mobile-frontend

Send all work to the one static backend.

  • url: "/work/"
    service: static-backend
    // [END sample_1]

// [START sample_2]

Send any path that begins with “simple-sample.appspot.com/mobile” to the mobile-frontend service.

  • url: "simple-sample.appspot.com/mobile*"
    service: mobile-frontend

Send any domain/sub-domain with a path that starts with “work” to the static backend service.

  • url: "/work"
    service: static-backend
    // [END sample_2]

// [START sample_3]

Matches the path "/fun", but not "/fun2" or "/fun/other".

  • url: "*/fun"
    service: mobile-frontend

Matches the hostname "customer1.myapp.com", but not "1.customer1.myapp.com".

  • url: "customer1.myapp.com/*"
    service: static-backend
    // [END sample_3]

Appengine Tools Uploadingdata (`docs/appengine/tools/uploadingdata.txt`)

// [START setting_up_remote_api_1]

  • url: /_ah/remote_api
    script: _go_app
    // [END setting_up_remote_api_1]

// [START setting_up_remote_api_2]
import _ "google.golang.org/appengine/remote_api"
// [END setting_up_remote_api_2]

// [START setting_up_remote_api_3]
gcloud app deploy app.yaml
// [END setting_up_remote_api_3]


Appengine Datastore Indexes Queries (`docs/appengine/datastore/indexes/queries.txt`)

// [START queries_and_indexes_example_1]
SELECT * FROM Person WHERE LastName = "Smith"
AND Height < 72
ORDER BY Height DESC
// [END queries_and_indexes_example_1]

// [START queries_and_indexes_example_2]
SELECT * FROM Person WHERE LastName = "Jones"
AND Height < 63
ORDER BY Height DESC
// [END queries_and_indexes_example_2]

// [START queries_and_indexes_example_3]
SELECT * FROM Person WHERE LastName = "Friedkin"
AND FirstName = "Damian"
ORDER BY Height ASC
// [END queries_and_indexes_example_3]

// [START queries_and_indexes_example_4]
SELECT * FROM Person WHERE LastName = "Blair"
ORDER BY FirstName, Height ASC
// [END queries_and_indexes_example_4]

// [START exploding_index_example_1]
SELECT * FROM Widget WHERE X=1 AND Y=2 ORDER BY Date
// [END exploding_index_example_1]


--- METRICS ---