How to write and deploy Go programs in Google Cloud

Google Cloud App Engine, in addition to writing programs in Python, Java, PHP, you can also use the Go language, here to teach you how to start. But we do not use App Engine to deploy, instead use Container Engine (still in Beta).

  1. It is recommended that you use Google Container Engine (docker) instead of App Engine, because the container will be cheaper and more flexible. Therefore, choose flexible environment (do beta test, no money) -> https:/ /cloud.google.com/appengine/docs/flexible/go/quickstart
  2. [Hypothesis 1] You have installed the go development environment. If you haven't already, click -> (install go), or go directly to Google Cloud Shell (>_) in your Google Cloud Console. There is also a free computer installed. Go language.

4. Use Google Cloud Shell as an example. (You can also test your go program on a local computer.) As shown below, if you execute goapp serve, select the Web preview icon in the upper left corner to display your http://0.0. .0.0:8080 Web content.

5. Execute the test program guestbook as follows

git clone https://github.com/GoogleCloudPlatform/appengine-guestbook-go.gitcd appengine-guestbook-go/git fetchgit checkout part4-usingdatastoregoapp serve

The results of the web page are as follows:

6. Deploy to the official website, https://oidc-1339.appspot.com/, where oidc-1339 is the Project ID of your current Google cloud. The following example uses the Mingderwang account as an example. If your username is alex, GOPATH will be changed. In /home/alex/gopath (for example, under Google Cloud Shell)

export GOPATH=/home/mingderwang/gopath
go get -u -d github.com/GoogleCloudPlatform/golang-samples/appengine_flexible/helloworld
cd $GOPATH/src/github.com/GoogleCloudPlatform/golang-samples/appengine_flexible/helloworld
aedeploy gcloud app deploy

7. We use the sample program provided by GOogle to test the deployment. There are two files app.yml and helloworld.go as follows; from app.yml env: flex, it is deployed in a flexible environment (the old name is Managed) VMs), not the App Engine environment!

$ cat app.yml

runtime: goenv: flex

$ cat helloworld.go

// Copyright 2015 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.
// Sample helloworld is a basic App Engine flexible app.package main
import ( "fmt" "log" "net/http")
func main() { http.HandleFunc("/", handle) http.HandleFunc("/_ah/health", healthCheckHandler) log.Print("Listening on port 8080") log.Fatal(http.ListenAndServe(":8080", nil))}
func handle(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/" {  http.NotFound(w, r)  return } fmt.Fprint(w, "Hello world!")}
func healthCheckHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "ok")}